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

asynchronous ajax call jquery example
how to make ajax call synchronous in javascript
jquery get
jquery ajax
$.get async false
jquery ajax get example
jquery ajax synchronous
return ajax response to variable

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?

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
    });
}

How to get jQuery to perform a synchronous, rather than , JavaScript is by nature asynchronous. In some early versions of Chrome and Firefox,you could do that using a flag in AJAX request [code]async: false  jQuery to perform a synchronous, rather than asynchronous, Ajax request - To implement a synchronous request, change the third parameter in the open method to false. async = false are used for quick testing.

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.

jQuery to perform a synchronous, rather than asynchronous, Ajax , jQuery to perform a synchronous, rather than asynchronous, Ajax request - To implement a synchronous request, change the third parameter in the open  The short answer is, You can’t. JavaScript is by nature asynchronous. In some early versions of Chrome and Firefox,you could do that using a flag in AJAX request

Excellent solution! I noticed when I tried to implement it that if I returned a value in the success clause, it came back as undefined. I had to store it in a variable and return that variable. This is the method I came up with:

function getWhatever() {
  // strUrl is whatever URL you need to call
  var strUrl = "", strReturn = "";

  jQuery.ajax({
    url: strUrl,
    success: function(html) {
      strReturn = html;
    },
    async:false
  });

  return strReturn;
}

jQuery.ajax(), Description: Perform an asynchronous HTTP (Ajax) request. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. 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. Then your callback can set some data before your mother function proceeds.

All of these answers miss the point that doing an Ajax call with async:false will cause the browser to hang until the Ajax request completes. Using a flow control library will solve this problem without hanging up the browser. Here is an example with Frame.js:

beforecreate: function(node,targetNode,type,to) {

    Frame(function(next)){

        jQuery.get('http://example.com/catalog/create/', next);
    });

    Frame(function(next, response)){

        alert(response);
        next();
    });

    Frame.init();
}

How to Return AJAX Response from Asynchronous JavaScript Call, Asynchronous JavaScript and XML, or Ajax, isn't a new technology in itself, and it's Note that while the “X” in Ajax represents XML, JSON is more You can think of synchronous methods like making a telephone call and If you are using jQuery, you can easily do this by setting the async option to false. 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. Then your callback can set some data before your mother function proceeds.

function getURL(url){
    return $.ajax({
        type: "GET",
        url: url,
        cache: false,
        async: false
    }).responseText;
}


//example use
var msg=getURL("message.php");
alert(msg);

How to Make Synchronous AJAX Calls in JQuery Quick Tips , Synchronous AJAX is probably an oxymoron. The A in Ajax stands for "​Asynchronous" . How to Make Synchronous AJAX Calls in JQuery This is usually better than having them wait for an entire Web page to refresh. By default, the $.ajax request in jQuery is set to asynchronous. The variable name is async and the value is set to true. This gave me a little confusion as well when first learning about it, so let’s go over it. Asynchronous vs. Synchronous. The default setting in jQuery. In my experience, Asynchronous should almost always do the trick.

AJAX Request, async: true (asynchronous) or false (synchronous). send(), Sends the GET is simpler and faster than POST, and can be used in most cases. However, always​  How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request? How do I check if an element is hidden in jQuery? How to manage a redirect request after a jQuery Ajax call ; Abort Ajax requests using jQuery ; How can I know which radio button is selected via jQuery?

How can I get jQuery to perform a synchronous, rather than , How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax I've added an Ajax call into this function using jQuery: The A in AJAX stands for asynchronous. That means sending the request (or rather receiving the response) is taken out of the normal execution flow. In your example, $.ajax returns immediately and the next statement, return result;, is executed before the function you passed as success callback was even called.

AJAX: Why Asynchronous Is Almost Always Better, When should you use asynchronous AJAX calls and when should you use synchronous. Find out more here. Here Mudassar Ahmed Khan has explained with an example, what is the difference between Synchronous (Sync) and Asynchronous (Async) Request (Call) in AJAX. This article will illustrate the difference between Synchronous (Sync) and Asynchronous (Async) Request (Call) in AJAX using jQuery.

Comments
  • The proper way to solve that would be to rewrite your widget's extension point use promises. This way would easily allow you to set up an asynchronous action (like an ajax request) as beforecreate.
  • I propose the Sajak function. Do we have a letter T? Yes vanna, give me 3 T's.
  • @Kos is spot on. Sync XHR is not required here
  • If people ask me for 1 piece of advice when starting javascript, I say: embrace the asynchronous character of javascript, don't try ty fight that.
  • You can try using embeddedjs.com
  • Exactly, it is impossible to use get(), post(), load() for synchronous calls. Only ajax() have "async" parameter, which can be set to "false".
  • @SLA80 Nope. Since jQuery 1.1: stackoverflow.com/questions/6849686/…
  • @qualidafial my comment is aimed at SLA80's incorrect comment; it is possible to use get(), post(), load() for synchronous calls.
  • async false is no longer supported for jQuery >= 1.8. Refer to api.jquery.com/jQuery.ajax
  • @ken async: false is still supported in jQuery >= 1.8. The ability to use async: false with jqXHR ($.Deferred) is what was deprecated. In other words, one must use the newer success/error/complete callback options, instead of the legacy methods, e.g., jqXHR.done().