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

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

    Firstly we should understand when we use $.ajax and when we use $.get/$.post

    When we require low level control over the ajax request such as request header settings, caching settings, synchronous settings etc.then we should go for $.ajax.

    $.get/$.post: When we do not require low level control over the ajax request.Only simple get/post the data to the server.It is shorthand of

    $.ajax({
      url: url,
      data: data,
      success: success,
      dataType: dataType
    });
    

    and hence we can not use other features(sync,cache etc.) with $.get/$.post.

    Hence for low level control(sync,cache,etc.) over ajax request,we should go for $.ajax

     $.ajax({
         type: 'GET',
          url: url,
          data: data,
          success: success,
          dataType: dataType,
          async:false
        });
    
    0 讨论(0)
  • 2020-11-21 05:43

    With async: false you get yourself a blocked browser. For a non blocking synchronous solution you can use the following:

    ES6/ECMAScript2015

    With ES6 you can use a generator & the co library:

    beforecreate: function (node, targetNode, type, to) {
        co(function*(){  
            let result = yield jQuery.get('http://example.com/catalog/create/' + targetNode.id + '?name=' + encode(to.inp[0].value));
            //Just use the result here
        });
    }
    

    ES7

    With ES7 you can just use asyc await:

    beforecreate: function (node, targetNode, type, to) {
        (async function(){
            let result = await jQuery.get('http://example.com/catalog/create/' + targetNode.id + '?name=' + encode(to.inp[0].value));
            //Just use the result here
        })(); 
    }
    
    0 讨论(0)
  • 2020-11-21 05:45

    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
        });
    }
    
    0 讨论(0)
  • 2020-11-21 05:45

    This is example:

    $.ajax({
      url: "test.html",
      async: false
    }).done(function(data) {
       // Todo something..
    }).fail(function(xhr)  {
       // Todo something..
    });
    
    0 讨论(0)
  • 2020-11-21 05:47

    this is my simple implementation for ASYNC requests with jQuery. I hope this help anyone.

    var queueUrlsForRemove = [
        'http://dev-myurl.com/image/1', 
        'http://dev-myurl.com/image/2',
        'http://dev-myurl.com/image/3',
    ];
    
    var queueImagesDelete = function(){
    
        deleteImage( queueUrlsForRemove.splice(0,1), function(){
            if (queueUrlsForRemove.length > 0) {
                queueImagesDelete();
            }
        });
    
    }
    
    var deleteImage = function(url, callback) {
        $.ajax({
            url: url,
            method: 'DELETE'
        }).done(function(response){
            typeof(callback) == 'function' ? callback(response) : null;
        });
    }
    
    queueImagesDelete();
    
    0 讨论(0)
  • 2020-11-21 05:49

    Since the original question was about jQuery.get, it is worth mentioning here that (as mentioned here) one could use async: false in a $.get() but ideally avoid it since asynchronous XMLHTTPRequest is deprecated (and the browser may give a warning):

    $.get({
      url: url,// mandatory
      data: data,
      success: success,
      dataType: dataType,
      async:false // to make it synchronous
    });
    
    0 讨论(0)
提交回复
热议问题