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

后端 未结 14 2322
暗喜
暗喜 2020-11-21 05:07

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

14条回答
  •  悲哀的现实
    2020-11-21 05:51

    Keep in mind that if you're doing a cross-domain Ajax call (by using JSONP) - you can't do it synchronously, the async flag will be ignored by jQuery.

    $.ajax({
        url: "testserver.php",
        dataType: 'jsonp', // jsonp
        async: false //IGNORED!!
    });
    

    For JSONP-calls you could use:

    1. Ajax-call to your own domain - and do the cross-domain call server-side
    2. Change your code to work asynchronously
    3. Use a "function sequencer" library like Frame.js (this answer)
    4. Block the UI instead of blocking the execution (this answer) (my favourite way)

提交回复
热议问题