Home > Article > Web Front-end > jquery post synchronization and asynchronous summary
I was testing recently and found that some of the effects were wrong. Finally, I found that it was a problem with the execution order of posts, so I studied it and wrote the following summary
1. Post was requested multiple times, solution:
Join a random number rand=""+Math.random()
$.post("/Control/webControl.ashx?rand ="+Math.random(), { Method: "LoginIn", Parems: ps }, function (data, textStatus) { $("#pOver").css("visibility", "hidden"); switch (data) { case "1": window.location = '/default.aspx'; break; case "0": $("#tips").text("找不到该用户"); u.focus(); changevcode(); errcount++; break; case "-1": $("#tips").text("用户或密码错误"); changevcode(); c.val("").focus(); errcount++; break; default: alert(data); } });
2. Because post defaults to an asynchronous request, but sometimes we will find that the request is required to appear immediately, but asynchronous will cause If it is executed suddenly later, something will go wrong
So, we need to add
$.ajaxSetup({ async : false });
3. If we need some synchronization and some asynchronous, then we should use the original ajax request, and then add async Set to false
$.ajax({ type : "post", url : "register/RegisterState", data : "test=" + test, async : false, success : function(data){ data = eval("(" + data + ")"); aDataSet = data; } });
The above is the detailed content of jquery post synchronization and asynchronous summary. For more information, please follow other related articles on the PHP Chinese website!