English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When sending and receiving data to the backend through AJAX, synchronization and asynchronous issues often arise. Since AJAX is loaded asynchronously by default, but sometimes a synchronous or synchronous effect is needed, there are two solutions.
Method one: Execute certain methods within callback functions, that is, execute after the successful return from the backend.
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 returns successfully. If it is placed outside of if(resp.code==0){} (but after $.getJSON();), there is a risk that the red part of the code will be executed before the data is returned from the backend.
Method two: Using standard AJAX transmission method
$.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&¤tPage!=1{ currentPage = currentPage - 1; } var para = { mypara :currentPage, startPage : currentPage, }); $('div.page-box').data('myPage').setOptions({data: para}); } } });
Note:This method is just a local synchronous transmission method, which will not affect other transmissions and is a relatively safe and recommended method
method.
There is another way:
// $.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 not recommended to use because it will affect other Ajax transmissions.
The above is a brief analysis of Ajax synchronous and asynchronous issues introduced by the editor, hoping it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)