Ajax callback data assigned to variables

Source: Internet
Author: User

I wrote this article because my previous Ztree article caused my research on js. If I don't understand it, I can first look at the data returned by Ztree above, because it is the same small example, the previous article contains all the code ,:

I declare the global variable tree and assign the callback data to the variables in the Success callback function. I thought this was also the case according to programming habits, but the problem occurred, let me briefly describe the lele.info method to print data on the console, because when alert returns data with json data, you cannot generate actual data with alert, this method is used to try Firefox. It seems that IE is not enough. I will not discuss it here. Firefox is installed with the Firebug plug-in. The printed result is,

The printed result does not exist. Then I use the following method, for example, I can print the data in the callback function,

At this time, I wondered why the data called back cannot be assigned a value to the global variable. Then I read the Jquery API and found the synchronous method, such:

Async: false can be added to ajax. Of course, in addition to this method, you can add $. fn. zTree. init ($ ("# treeDemo"), setting, tree); this code can also be written in the callback function to implement a tree menu, however, I still struggle with why the data department of the callback can assign values to variables. Next I did another experiment, for example:

I printed it in the callback and printed it out again. The result is as follows:

The following is printed first, and the problem is clear. Although I wrote ajax first, the following code is executed first, so the variable tree cannot be printed, in the callback, We can print the values assigned later. In summary, asynchronous requests cannot pass the returned values to global variables according to my first writing method, because JS only executes the current code in sequence. When a request is sent, it is a request response. Regardless of this, it only needs to continue executing the code in front of it, so it cannot assign values to variables. To send the request to a variable, you can use synchronization, that is, async: false, but the synchronous request will lock the browser. Other operations must be performed only after the request is completed. If you want to assign a value to the tree menu data of Ztree, I finally chose to initialize the data in the callback function. For more information, see the JS asynchronous principle, I will not discuss it much here. You can look at the Ztree code. Well, it's over.

For the first time, I wrote more formal technical articles as much as possible, and I did not write anything I had been studying. If the time permits, I will try my best to give you the previous summary. The comparative technology is shared and shared. I hope everyone can understand whether the technical skills are good or just getting started, I hope to help some friends in need. If you have any questions, please give me feedback. I will give you a prompt reply. 201771001325

Related Article

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.