How to do a synchronous json ajax call with jquery

11,856

By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding method

http://api.jquery.com/jQuery.ajax/

Share:
11,856
Benubird
Author by

Benubird

Updated on June 26, 2022

Comments

  • Benubird
    Benubird almost 2 years

    I have the following json ajax call:

    $.getJSON(server_data.secure_api+'/e/account/check/?callback=?', queryData,
        function(data) {
            has_error = (data.status == 'failure');
    });
    

    Which works perfectly, except that it is asynchronous. I now need to make it synchronous, because I need to pause the calling function until has_error is set. How do I do this?

    I have already tried using a .ajax call, like this:

    jQuery.ajax({
        url:        server_data.secure_api+'/e/account/check/?callback=?',
        data:       queryData,
        DataType:   'jsonp',
        success:    function(result) {
                    has_error = (data.status == 'failure');
                },
        async:      false
    });
    

    But it doesn't work! I've tried setting the DataType to json, jsonp, or not set; I've tried including the ?callback=? and I've tried leaving it off; none of this has worked. What am I doing wrong?

  • Benubird
    Benubird about 10 years
    This is happening as part of a form submit, which aborts if has_error. I could do it that way, but this would be tidier.
  • Benubird
    Benubird about 10 years
    So, are you saying that synchronous calls are no longer possible?
  • Jay Blanchard
    Jay Blanchard about 10 years
    That's pretty much what he is saying.
  • rookie
    rookie about 10 years
    If you're using AJAX why are you trying to do a standard form submit? Why not use AJAX for everything?
  • Benubird
    Benubird about 10 years
    Because I need to submit the data to another page, and doing an ajax request to save it followed by a redirect seems wasteful.
  • rookie
    rookie about 10 years
    Well those really are your only two legitimate options. Place the form submission inside the AJAX callback or avoid using a form submission at all.
  • rookie
    rookie about 10 years
    @Benubird - For future reference, just because someone is telling you something you don't want to hear is no reason not to give an up vote.
  • Benubird
    Benubird about 10 years
    You haven't told me anything useful - what callback function? where? how do I place code in it? I guess you are probably talking about jquery.deferred, but you haven't named it here. I can't give someone an upvote for saying RTFM, when they don't even link me to the manual in question. Upvotes are for useful answers; this answer is not useful to me.
  • rookie
    rookie about 10 years
    It is useful you just lack the knowledge to apply it. Which is fine but you could specify which option you are interested in learning more about and I'd be happy to extrapolate. Nowhere in my post did I say or imply "RTFM". You came up with that all on your own.
  • Stijn de Witt
    Stijn de Witt almost 9 years
    @SpencerRuport I'm getting the same vibe from it though... -1
  • rookie
    rookie over 8 years
    @StijndeWitt - What part of "specify which option you are interested in learning more about and I'd be happy to extrapolate" gives you a RTFM vibe?