Home > Web Front-end > JS Tutorial > Summary of methods and differences between synchronous and asynchronous processing in js_javascript skills

Summary of methods and differences between synchronous and asynchronous processing in js_javascript skills

WBOY
Release: 2016-05-16 17:07:03
Original
1207 people have browsed it

When using asynchronous requests, sometimes it is necessary to return the result of the asynchronous request to another js function. In this case, the request result will not be returned until the asynchronous request is returned. The js function where the request is sent has completed the subsequent operations, that is, the return has been executed. , which will cause the return result to be a null character.

Summary: To process the results returned by a send request after using an ajax request, it is best to use a synchronous request.

For example: In the following example, the return result will be incorrect because the ajax asynchronous request has not been executed yet and the function has already executed return.

Copy Code The code is 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 = ....
}

// There will also be errors when processing the data returned in ajax

return result ;
}

1 Asynchronous request method:
Copy code The code is 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 method
Copy code The code is as follows:

$.ajax({
url : 'your url',
data:{name:value},
cache : false,
async : false,
type : "POST",
dataType : 'json/xml/html',
success : function (result){
do something....
}
});
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template