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.