Scope of data returned by the Ajax callback method of dojo

Source: Internet
Author: User

 Today, we have a very simple Ajax function. It took a long time to solve it. The problem is that there is a callback function in Ajax that will return the server-side data. For details about the scope of the returned data, seeProgram:

 
Function Test () {var num = 1; require (["dojo/request"], function (request) {request ("request.html "). then (function (data) {num = data; // data = 2}) ;}); alert (Num); // 1}

In the above example, num or 1 has not changed. My idea is that the data value is not assigned to num at all. I learned from the Internet that the test () method has been executed and the Ajax callback function may not be executed yet. It should be a synchronous or asynchronous problem. Check the source code.

Defaultoptions = {data: NULL, query: NULL, Sync: false, method: 'get', headers: {'content-type ': 'application/X-WWW-form-urlencoded '}};


Found, by defaultSyncAttribute isFalseIs it asynchronous transmission, so as long as the asynchronous synchronization is changed, is it possible to use the returned data?SyncChangeTrue

 
Function Test () {var num = 1; require (["dojo/request"], function (request) {request ("request.html", {Sync: true }). then (function (data) {num = data; // data = 2}) ;}); alert (Num); // 2}

2 is returned, which indicates that it can be used. It is concluded that AJAX of dojo is asynchronous by default, but it can be changed to synchronous when there are other requirements.

As we are new to front-end development, it takes a lot of time to solve some very simple problems, hoping to improve these problems.




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.