Summary of jquery synchronous and asynchronous request issues

Source: Internet
Author: User

Summary of jquery synchronous and asynchronous request issues

    • Problem

     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

    • Solution Solutions

    • Programme I

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         }});
    • Programme II

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

    • Conclusion

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

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.