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&¤tPage!=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!