Home  >  Article  >  Web Front-end  >  Ajax execution sequence process and callback problem analysis_Basic knowledge

Ajax execution sequence process and callback problem analysis_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:47:061216browse

A global variable var JsonData;
I have an Ajax processing method here:
JScript code:

Copy code The code is as follows:

function GetJson(DataSourceName) {
$.ajax({
type: “post”,
url: “Ajax/AjaxData.ashx?MethodName= ” DataSourceName,
contentType: “application/json;”,
data: “”,
dataType: “json”,
success: function (Result) {
JsonData = Result;
},
error: function (result) {
alert("Error getting information list");
window.close();
}
});
return JsonData;
}

Then I have a class.
JScript code:
Copy code The code is as follows:

function DrawDropDownList(sFieldRuleMethod)
{
GetJson(sFieldSourceName);
var b = JsonData;
}

So, why can’t I always get JsonData when I execute DrawDropDownList?
I tracked the interruption points and found that the GetJson method will be entered after everything in the DrawDropDownList method has been executed.
Is there any way to get the Result data obtained in GetJson?
Do not copy code in
The code is as follows:

success: function (Result) {
//Do Something
},

I just want to use the obtained data, because GetJson is a general method and I don’t want to execute a single logic in it.
Cannot return in callback, and needs to be synchronized, that’s it!
Another way of letting go is not recommended for synchronization. I need to add a function parameter to my function as a callback function and pass the ajax result to the function. The following code details:
Copy code The code is as follows:

function GetJson(DataSourceName,callback) {
$.ajax({
type: “ post",
url: "Ajax/AjaxData.ashx?MethodName=" DataSourceName,
contentType: "application/json;",
data: "",
dataType: "json",
success: function (Result) {
JsonData = Result;
callback(JsonData)
},
error: function (result) {
alert("Error getting information list") ;
window.close();
}
});
//return JsonData;
}
Statement:
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