How to block ajax call (I want it to block)

Ajax uses callbacks because it is A synchronous.

I want my call to the remote URL block until some answer is found , just like in Ajax, but without the asynchronous part, or should I say that I want to make a JAX call.

Is there any method to do the following (uses jQuery) (... a solution with jQuery or something else):

 function get_data() { $.ajax({ type : "POST", url : "/foo" }).done(function(data, textStatus, jqXHR) { return data; }).fail(function(jqXHR, textStatus) { return null; }); } var data = get_data(); // process `data` 

I'm just curious - I want to study.

In fact, there are times when locking until the answer works well. I'm not saying that I want the browser to lock, only a script.

+6
source share
1 answer

You can simply set async : false boolean when using jQuery ( check docs ). Note: Starting with jQuery 1.8, using async: false with jqXHR ($ .Deferred) is deprecated; you must use full / successful / error callbacks.

If you do not want to use jQuery or want to know what is happening under the hood, read this .

 xmlhttp.open("GET","ajax_info.txt",false); xmlhttp.send(); document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 

Surprise why you do not want it to be asynchronous, though ...

+10
source

Source: https://habr.com/ru/post/924263/


All Articles