Manipulation of 'success' data breaks recursive ajax
I am writing an automatic script to do crawl jobs. I put the ajax function in a recursive function to automate the process. The whole thing works that the ajax function keeps fetching URLs from the list array. However, it stops working after I put this code: $("#myframe").html(temp.body);
, where temp
is the 'success' data and temp.body
is a part of the json response. Then, the code stops at the second item of the list. Please help me with the data manipulation of recursive ajax. Thank you.
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]);
The full code is here.
ajax
add a comment |
I am writing an automatic script to do crawl jobs. I put the ajax function in a recursive function to automate the process. The whole thing works that the ajax function keeps fetching URLs from the list array. However, it stops working after I put this code: $("#myframe").html(temp.body);
, where temp
is the 'success' data and temp.body
is a part of the json response. Then, the code stops at the second item of the list. Please help me with the data manipulation of recursive ajax. Thank you.
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]);
The full code is here.
ajax
First, please reduce your code to the absolute minimum that is needed to reproduce your issue. Remove all the DOM manipulation and anything else that has nothing to do with the problem at hand. Second, please put all the remaining code into the question here, instead of linking to an external site. Questions here should be self-contained and not depend on external resources that might go away at any time.
– Tomalak
Jan 2 at 12:26
Sorry for submitting the problem too quickly. I cleaned up the code so the problem is easy to understand now.
– Louis55
Jan 2 at 15:12
Are you sure that thehttps://cors-proxy.htmldriven.com/
call is even successful? Your code only has a "success" callback, which means if the Ajax call returns an HTTP error, the callback function that does the recursive call will not be executed. Check the network tab in your browser's debugging tools to find out if the calls are successful. Add an "error" callback function to your code (you should generally do that for all Ajax calls. Letting errors go unhandled is not a good idea).
– Tomalak
Jan 2 at 17:00
Also, you can't have more than one HTML element with the same ID. Your code would adddiv#myframe
over and over again, that's not going to work very well. Use unique IDs, or don't use IDs at all. For what you do here,$("<div>").html(temp.body).appendTo("body");
is completely sufficient.
– Tomalak
Jan 2 at 17:18
@Tomalak, I finally figured it out. First, thank you for telling me to check the status of the response (it is indeed successful). Second, the repeated ID won't be a problem since it will be removed later. Lastly, the problem is that the javascript inside the response starts to execute once the response is put in the div. Therefore, my solution is to do a replace at the first place.
– Louis55
Jan 3 at 5:10
add a comment |
I am writing an automatic script to do crawl jobs. I put the ajax function in a recursive function to automate the process. The whole thing works that the ajax function keeps fetching URLs from the list array. However, it stops working after I put this code: $("#myframe").html(temp.body);
, where temp
is the 'success' data and temp.body
is a part of the json response. Then, the code stops at the second item of the list. Please help me with the data manipulation of recursive ajax. Thank you.
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]);
The full code is here.
ajax
I am writing an automatic script to do crawl jobs. I put the ajax function in a recursive function to automate the process. The whole thing works that the ajax function keeps fetching URLs from the list array. However, it stops working after I put this code: $("#myframe").html(temp.body);
, where temp
is the 'success' data and temp.body
is a part of the json response. Then, the code stops at the second item of the list. Please help me with the data manipulation of recursive ajax. Thank you.
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]);
The full code is here.
ajax
ajax
edited Jan 2 at 15:05
Louis55
asked Jan 2 at 12:03
Louis55Louis55
168211
168211
First, please reduce your code to the absolute minimum that is needed to reproduce your issue. Remove all the DOM manipulation and anything else that has nothing to do with the problem at hand. Second, please put all the remaining code into the question here, instead of linking to an external site. Questions here should be self-contained and not depend on external resources that might go away at any time.
– Tomalak
Jan 2 at 12:26
Sorry for submitting the problem too quickly. I cleaned up the code so the problem is easy to understand now.
– Louis55
Jan 2 at 15:12
Are you sure that thehttps://cors-proxy.htmldriven.com/
call is even successful? Your code only has a "success" callback, which means if the Ajax call returns an HTTP error, the callback function that does the recursive call will not be executed. Check the network tab in your browser's debugging tools to find out if the calls are successful. Add an "error" callback function to your code (you should generally do that for all Ajax calls. Letting errors go unhandled is not a good idea).
– Tomalak
Jan 2 at 17:00
Also, you can't have more than one HTML element with the same ID. Your code would adddiv#myframe
over and over again, that's not going to work very well. Use unique IDs, or don't use IDs at all. For what you do here,$("<div>").html(temp.body).appendTo("body");
is completely sufficient.
– Tomalak
Jan 2 at 17:18
@Tomalak, I finally figured it out. First, thank you for telling me to check the status of the response (it is indeed successful). Second, the repeated ID won't be a problem since it will be removed later. Lastly, the problem is that the javascript inside the response starts to execute once the response is put in the div. Therefore, my solution is to do a replace at the first place.
– Louis55
Jan 3 at 5:10
add a comment |
First, please reduce your code to the absolute minimum that is needed to reproduce your issue. Remove all the DOM manipulation and anything else that has nothing to do with the problem at hand. Second, please put all the remaining code into the question here, instead of linking to an external site. Questions here should be self-contained and not depend on external resources that might go away at any time.
– Tomalak
Jan 2 at 12:26
Sorry for submitting the problem too quickly. I cleaned up the code so the problem is easy to understand now.
– Louis55
Jan 2 at 15:12
Are you sure that thehttps://cors-proxy.htmldriven.com/
call is even successful? Your code only has a "success" callback, which means if the Ajax call returns an HTTP error, the callback function that does the recursive call will not be executed. Check the network tab in your browser's debugging tools to find out if the calls are successful. Add an "error" callback function to your code (you should generally do that for all Ajax calls. Letting errors go unhandled is not a good idea).
– Tomalak
Jan 2 at 17:00
Also, you can't have more than one HTML element with the same ID. Your code would adddiv#myframe
over and over again, that's not going to work very well. Use unique IDs, or don't use IDs at all. For what you do here,$("<div>").html(temp.body).appendTo("body");
is completely sufficient.
– Tomalak
Jan 2 at 17:18
@Tomalak, I finally figured it out. First, thank you for telling me to check the status of the response (it is indeed successful). Second, the repeated ID won't be a problem since it will be removed later. Lastly, the problem is that the javascript inside the response starts to execute once the response is put in the div. Therefore, my solution is to do a replace at the first place.
– Louis55
Jan 3 at 5:10
First, please reduce your code to the absolute minimum that is needed to reproduce your issue. Remove all the DOM manipulation and anything else that has nothing to do with the problem at hand. Second, please put all the remaining code into the question here, instead of linking to an external site. Questions here should be self-contained and not depend on external resources that might go away at any time.
– Tomalak
Jan 2 at 12:26
First, please reduce your code to the absolute minimum that is needed to reproduce your issue. Remove all the DOM manipulation and anything else that has nothing to do with the problem at hand. Second, please put all the remaining code into the question here, instead of linking to an external site. Questions here should be self-contained and not depend on external resources that might go away at any time.
– Tomalak
Jan 2 at 12:26
Sorry for submitting the problem too quickly. I cleaned up the code so the problem is easy to understand now.
– Louis55
Jan 2 at 15:12
Sorry for submitting the problem too quickly. I cleaned up the code so the problem is easy to understand now.
– Louis55
Jan 2 at 15:12
Are you sure that the
https://cors-proxy.htmldriven.com/
call is even successful? Your code only has a "success" callback, which means if the Ajax call returns an HTTP error, the callback function that does the recursive call will not be executed. Check the network tab in your browser's debugging tools to find out if the calls are successful. Add an "error" callback function to your code (you should generally do that for all Ajax calls. Letting errors go unhandled is not a good idea).– Tomalak
Jan 2 at 17:00
Are you sure that the
https://cors-proxy.htmldriven.com/
call is even successful? Your code only has a "success" callback, which means if the Ajax call returns an HTTP error, the callback function that does the recursive call will not be executed. Check the network tab in your browser's debugging tools to find out if the calls are successful. Add an "error" callback function to your code (you should generally do that for all Ajax calls. Letting errors go unhandled is not a good idea).– Tomalak
Jan 2 at 17:00
Also, you can't have more than one HTML element with the same ID. Your code would add
div#myframe
over and over again, that's not going to work very well. Use unique IDs, or don't use IDs at all. For what you do here, $("<div>").html(temp.body).appendTo("body");
is completely sufficient.– Tomalak
Jan 2 at 17:18
Also, you can't have more than one HTML element with the same ID. Your code would add
div#myframe
over and over again, that's not going to work very well. Use unique IDs, or don't use IDs at all. For what you do here, $("<div>").html(temp.body).appendTo("body");
is completely sufficient.– Tomalak
Jan 2 at 17:18
@Tomalak, I finally figured it out. First, thank you for telling me to check the status of the response (it is indeed successful). Second, the repeated ID won't be a problem since it will be removed later. Lastly, the problem is that the javascript inside the response starts to execute once the response is put in the div. Therefore, my solution is to do a replace at the first place.
– Louis55
Jan 3 at 5:10
@Tomalak, I finally figured it out. First, thank you for telling me to check the status of the response (it is indeed successful). Second, the repeated ID won't be a problem since it will be removed later. Lastly, the problem is that the javascript inside the response starts to execute once the response is put in the div. Therefore, my solution is to do a replace at the first place.
– Louis55
Jan 3 at 5:10
add a comment |
1 Answer
1
active
oldest
votes
Before the start of any 'data manipulation', any script within the response should be removed first to prevent possible ajax error.
var cont = temp.body.replace(/<script.*?</script>/g,'');
$("#myframe").html(cont);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006016%2fmanipulation-of-success-data-breaks-recursive-ajax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Before the start of any 'data manipulation', any script within the response should be removed first to prevent possible ajax error.
var cont = temp.body.replace(/<script.*?</script>/g,'');
$("#myframe").html(cont);
add a comment |
Before the start of any 'data manipulation', any script within the response should be removed first to prevent possible ajax error.
var cont = temp.body.replace(/<script.*?</script>/g,'');
$("#myframe").html(cont);
add a comment |
Before the start of any 'data manipulation', any script within the response should be removed first to prevent possible ajax error.
var cont = temp.body.replace(/<script.*?</script>/g,'');
$("#myframe").html(cont);
Before the start of any 'data manipulation', any script within the response should be removed first to prevent possible ajax error.
var cont = temp.body.replace(/<script.*?</script>/g,'');
$("#myframe").html(cont);
answered Jan 3 at 5:15
Louis55Louis55
168211
168211
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006016%2fmanipulation-of-success-data-breaks-recursive-ajax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
First, please reduce your code to the absolute minimum that is needed to reproduce your issue. Remove all the DOM manipulation and anything else that has nothing to do with the problem at hand. Second, please put all the remaining code into the question here, instead of linking to an external site. Questions here should be self-contained and not depend on external resources that might go away at any time.
– Tomalak
Jan 2 at 12:26
Sorry for submitting the problem too quickly. I cleaned up the code so the problem is easy to understand now.
– Louis55
Jan 2 at 15:12
Are you sure that the
https://cors-proxy.htmldriven.com/
call is even successful? Your code only has a "success" callback, which means if the Ajax call returns an HTTP error, the callback function that does the recursive call will not be executed. Check the network tab in your browser's debugging tools to find out if the calls are successful. Add an "error" callback function to your code (you should generally do that for all Ajax calls. Letting errors go unhandled is not a good idea).– Tomalak
Jan 2 at 17:00
Also, you can't have more than one HTML element with the same ID. Your code would add
div#myframe
over and over again, that's not going to work very well. Use unique IDs, or don't use IDs at all. For what you do here,$("<div>").html(temp.body).appendTo("body");
is completely sufficient.– Tomalak
Jan 2 at 17:18
@Tomalak, I finally figured it out. First, thank you for telling me to check the status of the response (it is indeed successful). Second, the repeated ID won't be a problem since it will be removed later. Lastly, the problem is that the javascript inside the response starts to execute once the response is put in the div. Therefore, my solution is to do a replace at the first place.
– Louis55
Jan 3 at 5:10