jquery Ajax asynchronous and synchronous from the acquired value

Source: Internet
Author: User

Recently, using jquery Ajax, we found that some effects were incorrect, and the JSON string returned after the Ajax request came back, but there was a problem with the order of execution.

var false ; $.post ('/home/detectcachedlogininfor/',function  (result) {if  ( result.errormsg) {iftrue' json '); if (isreload) {openreloginform (); return false ;}

It is found that the returned number is result.errormsg, but Isreload cannot give the assignment true, the following if (Isreload) {...} cannot be executed smoothly

The final discovery is the asynchronous execution order of the post, so the following research is done, and a summary is written

--------because the post defaults to an asynchronous request, the data is returned asynchronously instead of sequentially executing until the data is returned before the next step is performed.

So we'll get the value of the method, Ajax--async:false can

varIsreload =false; $.ajax ({URL:'/home/detectcachedlogininfor/', Async:false,//Note that synchronization is required here, because after the data is returned, the first bar of the result is selectedType: "POST", DataType:"JSON", Success:function(Result) {if(result.errormsg) {alert (result.errormsg); isreload =true; }}});if(isreload) {openreloginform ();return false;}

jquery Ajax asynchronous and synchronous from the acquired value

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.