Summary of jquery synchronous and asynchronous request issues
doing projects these days, writing scripts encountered a problem, Is the jquery asynchronous request and synchronization request execution order is not executed in code order, but the final execution results in the addition of data error, add to the null value, this How to endure, so I went to find jquery API, finally know the original jquery default asynchronous request, prevent data card dead, finally let me find this goods async , when Async:true, Ajax requests are asynchronous. When Async:true, is synchronous, but I have a question, how to set, where this setting, with $.ajax to write this operation, no, not too troublesome, how to solve the
With the $.ajax method, you can set the request method, remember to set async to False is synchronous, the following is the code case
$.ajax ({ type : "GET",//Request mode url : '/address /getparentid ',//request url data : {parentid : parentid},//Request parameters dataType : ' json ',//return data type async : false, // Set to False is whether the synchronization request cache : false,//cache, the default true success : function (AddressList) {//Success Event if (addresslist && addresslist.length > 0) { $.each (addresslist, function (Index, item) { array.push ({ id : item.id, name : item.address, }); }); } }, error : function (XMLHttpRequest , textstatus, errortHrown) {//Failure Event // Normally textstatus and Errorthown have only one of the values this ; // the options for this ajax request }});
Using $.ajaxsetup to set request properties
Set the synchronization $.ajaxsetup ({async:false}); var array = [];$.get ('/address/getparentid ', {parentid:parentid},function (ad Dresslist) {//Loop Add Data $.each (AddressList, function (index, item) {Array.push ({id:item.id, Name:item. (address,});});
This will be OK, the request is synchronized
The first blog post, the shortcomings please point out that I will improve, thank you
Reference: Jquery.ajax Error Debugging Help information
This article is from the "Confession of Tears" blog, please be sure to keep this source http://chenzf.blog.51cto.com/7941390/1678312
Summary of jquery synchronous and asynchronous request issues