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

后端 未结 14 2291
暗喜
暗喜 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
        });
    

提交回复
热议问题