My code is something like this:
for (var i = 0; i < stf_file_names.length; i++) {
var temp_file_name = stf_file_names[i];
$.ajax({
type: "POST",
url: "php_scripts/some_script.php",
data: {
stf_file_name: temp_file_name
},
timeout: 600000,
success: function (response) {
console.log("SUCCESS : ", response);
//pausecomp(2000);
}
});
}
Here, some_script.php updates a database in the backend and echo's the primary key of the updated row, which is a number. But when I'm logging using the success function, I can see that it is logging only the primary key echoed by the last ajax call multiple times.
But if I use some kind of sleep function, which is pausecomp() in this case, it prints different the primary keys echoed.
I have looked at multiple stackoverflow questions regarding this and have not been to solve it.
async: false
will do the job
$.ajax({
type: "POST",
async: false,
url: "php_scripts/some_script.php",
data:
However, this is not recommended, Better to make a loop by calling a function recursively from success.
Here is the example.
i=0;
function loop_stf_file_names(i){
var temp_file_name = stf_file_names[i];
$.ajax({
type: "POST",
url: "php_scripts/some_script.php",
async: false,
data: {
stf_file_name: temp_file_name
},
timeout: 600000,
success: function (response) {
console.log("SUCCESS : ", response);
if( i < stf_file_names.length ){
loop_stf_file_names( ++i );
}
}
});
}
$.ajax()
is a async function.
By looping over ajax you are most probably sending the same data in all requests, due to which you are receiving same key for all requests.
Just sure , you send the next request when you have received the response from first request.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.