Analysis and Solution of Ajax synchronization and Asynchronization, and analysis of ajax synchronization and Asynchronization

Source: Internet
Author: User

Analysis and Solution of Ajax synchronization and Asynchronization, and analysis of ajax synchronization and Asynchronization

When sending and receiving data to the backend through ajax, synchronous Asynchronization often occurs. Because ajax is loaded asynchronously by default, but sometimes synchronization or synchronization is required, there are two solutions.

Solution 1: Put some methods in the callback function for execution, that is, wait until the return from the background is successful and then execute.

Example:

$.getJSON("/data-access/sens-config/IPandPortSel",{},function(resp){if(resp.code==0){$.each(resp.data,function(i,obj){option_net_type += addOption(obj);});$("#edit-addr_id").append(option_net_type);addr_idOld = $('#edit-addr_id').val(addr_id);}});

The red part must be executed after the data is returned successfully. if it is placed in if (resp. code = 0) {} outside (but put it in $. getJSON (); later), if data is not returned from the background, the code in red is executed.

Method 2: Use standard ajax Transmission

$. Ajax ({type: "post", url: "/data-access/manufacturer/deleteBranch", data: data, async: false, // cancel asynchronous success: function (resp) {if (resp. code = 0) {if (ids. length> = currentListNum & currentPage! = 1) {currentPage = currentPage-1;} var para = {mypara: currentPage, startPage: currentPage, };$ ('div. page-box '). data ('mypage '). setOptions ({data: para });}}});

Note:This method is only a local synchronous transmission mode and does not affect other transmission modes. It is a safer and recommended method.

Method.

Another method exists:

// $.ajaxSettings.async = false;// $.getJSON("/data-access/ip-config/deleteBranch",data,function(resp){// if(resp.code==0){// if(ids.length>=currentListNum&&currentPage!=1){// currentPage = currentPage - 1;// }// var para = {// mypara :currentPage,// startPage : currentPage,// };// $('div.page-box').data('myPage').setOptions({data: para});// }// });// $.ajaxSettings.async = true;

This method is global and is not recommended because it will affect other ajax transmission.

The above is an analysis of Ajax synchronization and Asynchronization introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message. The editor will reply to you in time!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.