Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

I have a JavaScript widget which provides standard extension points. One of them is the beforecreate function. It should return false to prevent an item from being created.

I've added an Ajax call into this function using jQuery:

beforecreate: function (node, targetNode, type, to) {   jQuery.get('http://example.com/catalog/create/' + targetNode.id + '?name=' + encode(to.inp[0].value),    function (result) {     if (result.isOk == false)          alert(result.message);   }); } 

But I want to prevent my widget from creating the item, so I should return false in the mother-function, not in the callback. Is there a way to perform a synchronous AJAX request using jQuery or any other in-browser API?

like image 840
Artem Tikhomirov Avatar asked Sep 25 '08 13:09

Artem Tikhomirov


People also ask

How can make AJAX call synchronous in jQuery?

ajax({ type: "POST", async: "false", url: "checkpass. php", data: "password="+password, success: function(html) { var arr=$. parseJSON(html); if(arr == "Successful") { return true; } else { return false; } } }); $. ajaxSetup({async: true});

Can AJAX requests be made synchronous?

AJAX can access the server both synchronously and asynchronously: Synchronously, in which the script stops and waits for the server to send back a reply before continuing. Asynchronously, in which the script allows the page to continue to be processed and handles the reply if and when it arrives.

Is AJAX synchronous or asynchronous?

Ajax requests are Asynchronous by nature, but it can be set to Synchronous , thus, having the codes before it, execute first.

What is jQuery AJAX synchronous?

Synchronous AJAX call is made when async setting of jQuery AJAX function is set to false while Asynchronous AJAX call is made when async setting of jQuery AJAX function is set to true. Default value of the async setting of jQuery AJAX function is true. jQuery Synchronous AJAX call.


2 Answers

You can put the jQuery's Ajax setup in synchronous mode by calling

jQuery.ajaxSetup({async:false}); 

And then perform your Ajax calls using jQuery.get( ... );

Then just turning it on again once

jQuery.ajaxSetup({async:true}); 

I guess it works out the same thing as suggested by @Adam, but it might be helpful to someone that does want to reconfigure their jQuery.get() or jQuery.post() to the more elaborate jQuery.ajax() syntax.

like image 39
Sydwell Avatar answered Sep 29 '22 20:09

Sydwell


From the jQuery documentation: you specify the asynchronous option to be false to get a synchronous Ajax request. Then your callback can set some data before your mother function proceeds.

Here's what your code would look like if changed as suggested:

beforecreate: function (node, targetNode, type, to) {     jQuery.ajax({         url: 'http://example.com/catalog/create/' + targetNode.id + '?name=' + encode(to.inp[0].value),         success: function (result) {             if (result.isOk == false) alert(result.message);         },         async: false     }); } 
like image 108
Adam Bellaire Avatar answered Sep 29 '22 19:09

Adam Bellaire