jQuery AJAX synchronous but "pauses" tab till finished [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I'm trying to send a few AJAX requests. It has to collect the data into a variable, and after it's done, it has to output the variable. If I don't do it synchronously, it will just output the variable before the requests are done. Is is possible to keep using the page, while the requests are running? The code could look something like this:
for (var i=0;i<10;i++) {
$.ajax({
type: "GET",
url: "http://google.com",
async: false,
data: "",
success:function(data){
total += $(data);
alert(total);
}
}
That should load http://google.com/ 10 times, and output data into a variable called total. It is not my code I use (I like to keep it as private as possible), but only an example.
While it's loading Google 10 times, the page pauses because of async: false. Is is possible to make the page still work, and basically do the AJAX in the background? While it's loading, I can't do anything on the page.

If I don't do it synchronously, it will just output the variable before the requests are done.
No, it will output the variable 10 times after each request is done. To avoid that, use counter for how many requests have finished:
for (var i=0;i<10;i++) {
$.ajax({
type: "GET",
url: "http://google.com",
async: true,
data: "",
success:function(data){
total += $(data);
if (!--i)
alert(total);
}
}
}

Related

Simplest solution for Synchronous XMLHttpRequest is deprecated and add a waiting circle gif animation while javascript do ajax call to php [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
My current code is :
var dataReceiveString = $.ajax({
url: "getDataReceiveString.php?"+param,
dataType: "text",
async: false
}).responseText;
// code that make use of dataReceiveString ;
The obvious choice of changing false to true does not work.
How should the code be rearranged ?
Also to add a waiting circle gif animation while javascript do ajax call to php.
edit : question is different from Execute function after Ajax call is complete
as avoiding use of async:false.
edit : question is different from
How do I return the response from an asynchronous call?
as it's too generic and not targetted to my question.
If you want to make it sync you can always rely to async functions:
async function doAjax(args) {
let result;
try {
result = await $.ajax({
url: ajaxurl,
type: 'POST',
data: args
});
// code that make use of dataReceiveString
return result;
} catch (error) {
console.error(error);
}
}

Javascript ajax global variable undefined [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I try to get some info from external php with ajax and use the data but my global var return undefined.I try with async false but is deprecated.If i console in success function it has value outside is undefined.
var pkz;
$(function(){
$.ajax({
type:'GET',
url: '/user/cred',
success: function (data){
pkz=data;
}
});
console.log(pkz);
The problem with your code is that the execution is asyncronous. When you run console.log, the ajax did not finished. Put it inside the ajax callback.
The ajax do not block the execution. It continue without finished. You SHOULD use callbacks.
If you want to use ajax syncronous:
$.ajax({
type: "GET",
url: remote_url,
async: false
})
This is a horrible solution.

$.post javascript function in loop [duplicate]

This question already has answers here:
Jquery call another function after ajax in loop is done
(1 answer)
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I am writing a code in which user checks multiple check boxes and those users assign to the selected division. I am using $.post function in a loop of check boxes. Everything is working fine and checked users are being assigned to the selected division. Now, the issue is that I want to print a response after $.post function but everything I write after this post function, javascript executes it before that function and that's why, I am unable to print the response. Please check the below code for your convenience and suggest a possible solution.
Thanks a lot
function assign_user(div_id){
//alert(div_id);
var favorite = [];
$.each($("input[name='user']:checked"), function(){
value = $(this).val();
$.post("../db/assign_user.php",{div_id:div_id,value:value},
function(x){
});
// I want to print final response here but it executes before post function
});
}
Ajax requests are asynchronous. They are sent to the server, then the javascript continues on without waiting for a response. If you type your Ajax request like:
$.ajax({
url: "../db/assign_user.php",
type: 'POST',
data: {div_id:div_id,value:value},
success: function(data){
alert(data);
}
});
then in the success function, you can handle or alert the response.

Why is the result of my Ajax request empty although the request goes through ok? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
My alert shows an "Undefined" with the following code:
var nonce = (function () {
$.ajax({
url: site_url + '/api/get_nonce/?controller=user&method=register&apikey=XXX&callback=?',
type: "GET",
dataType: 'json',
success: function(data){
nonce = data.nonce;
}
});
return nonce;
})();
alert(nonce);
The JSON is:
?({"status":"ok","controller":"user","method":"register","nonce":"XXX"})
What I'm doing wrong?
Ajax calls are asynchronous. At the point you are doing the return nonce; the result hasn't arrived yet - or at least isn't guaranteed to.
You'd need to put the alert(nonce);, and anything else you want to do with the result, inside the success handler of the Ajax call.
In the long run, you also want an error callback to handle when something goes wrong.

How to block thread on getJSON [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I have two web services calls I need to make from my JQuery 1.9.1 app. I need the results from the first one before I make the second call.
It appears that my getJSON requests do not block the thread. I'm told that javascript is not multi-threaded, so how do I block between these server calls?
jQuery's ajax functions all return a jqXHR object that implement the Promise interface.
Instead of blocking an asynchronous request (and everything else), nest your requests like so
$.ajax({...})
.done(function( data ) {
// second request
$.ajax({...});
});
Well, you don't need to block threads, that's old school.
You have two options:
Make the ajax call syncronous.
Cascade the ajax calls, so the second is made only once the first one is completed.
I recommend the second approach, because that is the right way to do it.
/* Call 1 */
$.ajax({
url: 'first url to call',
data: 'first data sent to the server',
success: function (results1){
// Do something with the results
/* make the second call */
$.ajax({
url: 'sencond url to call'
data: 'second data sent to the server'
success: function(results2){
// Do something when all completed
}
});
}
});
Call your 1st ajax request in async false mode to server
$.ajax({
url: Url,
dataType: 'json',
async: false,
data: myData,
success: function(data) {
//stuff
}
});

Categories