首页 > 解决方案 > 操作“成功”数据会破坏递归 ajax

问题描述

我正在编写一个自动脚本来执行爬网作业。我将 ajax 函数放在递归函数中以自动化该过程。ajax 函数不断从列表数组中获取 URL,这一切都有效。但是,在我输入此代码后它停止工作:$("#myframe").html(temp.body);temp“成功”数据在哪里,并且temp.body是 json 响应的一部分。然后,代码停在列表的第二项。请帮我处理递归ajax的数据操作。谢谢你。

var idlist = ["10001","10002","10003"];
var index = 0;
function job(id) {
  $.get("https://cors-proxy.htmldriven.com/?url=http://www.olgame.tw/sds/robot_detail.php?id="+id, function( temp ) {
    $("body").append('<div id="myframe"></div>');
    $("#myframe").html(temp.body);  //This line causes problem. The entire code stops at here.
    $("div#myframe").remove();
    if (idlist[index+1] != undefined) {
      index++;
      job(idlist[index]);
    }  //index stops at '1'.
  });
}
job(idlist[index]);

完整的代码在这里

标签: ajax

解决方案


在开始任何“数据操作”之前,应首先删除响应中的任何脚本,以防止可能的 ajax 错误。

var cont = temp.body.replace(/<script.*?<\/script>/g,'');
$("#myframe").html(cont);

推荐阅读