When using an asynchronous request, sometimes it is necessary to return the result of the asynchronous request to another JS function, in which case there will be an asynchronous request return request result, the request is in the JS function has performed the subsequent operation, that is, has been executed, which will cause the return result is null character.
Summary: To handle the results returned by a send request after using an AJAX request, it is best to use a synchronization request.
For example, the following example shows an incorrect return result because the Ajax asynchronous request has not been executed and the function has been returned.
Copy Code code as follows:
function fn () {
var result = "";
$.ajax ({
URL: ' Your URL ',
Data:{name:value},
Cache:false,
Async:true,
Type: "POST",
Success:function (data) {
Do something ....
result = ....
}
Processing of the data returned in Ajax can also be an error
return result;
}
1 Asynchronous Request mode:
Copy Code code as follows:
$.ajax ({
URL: ' Your URL ',
Data:{name:value},
Cache:false,
Async:true,
Type: "POST",
DataType: ' json/xml/html ',
Success:function (Result) {
Do something ....
}
});
2 Synchronous Request Mode
Copy Code code as follows:
$.ajax ({
URL: ' Your URL ',
Data:{name:value},
Cache:false,
Async:false,
Type: "POST",
DataType: ' json/xml/html ',
Success:function (Result) {
Do something ....
}
});