When invoking a jquery Ajax method, we sometimes need the method to return a value or assign a value to a global variable. But we found out that we didn't get the value we wanted when the program was done, and it's probably because you're using an AJAX asynchronous call Async:true (by default), Such as:
Copy Code code as follows:
function Managecommenttext (text) {
var result = text;
$.ajax ({
Data: "Get",
URL: "Getcomments.aspx",
Data: "type=gettext&commenttext=" + text,
Cache:false,
Async:false,
Success:function (data) {
result = data;
}
})
return result;
The above method is a synchronous invocation of Ajax, which returns the call of result to complete the method only after the data value has been obtained and assigned to cause. If set to Async:true,
The result is returned without waiting for the data value to be obtained.
Another solution is to write your code directly into the success method. (according to your business not all can be written directly into the success).
Note: If set to Async:false, the advantages of Ajax asynchronous are lost Oh.