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

后端 未结 14 2279
暗喜
暗喜 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:59

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

提交回复
热议问题