A global variable var JsonData;
Here is an Ajax processing method:
JScript code:
Copy codeThe Code is as follows:
Function GetJson (DataSourceName ){
$. Ajax ({
Type: "post ",
Url: "Ajax/AjaxData. ashx? MethodName = "+ CENAME,
ContentType: "application/json ;",
Data: "",
DataType: "json ",
Success: function (Result ){
JsonData = Result;
},
Error: function (result ){
Alert ("An error occurred while obtaining the information List ");
Window. close ();
}
});
Return JsonData;
}
Then I have a class.
JScript code:
Copy codeThe Code is as follows:
Function DrawDropDownList (sFieldRuleMethod)
{
GetJson (sFieldSourceName );
Var B = JsonData;
}
So why can't I get JsonData when I execute DrawDropDownList?
After logging on to the breakpoint, I found that the GetJson method will be entered after all the operations in the DrawDropDownList method are completed,
Is there any way to obtain the Result data in GetJson?
Do not
Copy codeThe Code is as follows:
Success: function (Result ){
// Do Something
},
I just want to use the obtained data, because GetJson is a common method and does not want to execute a single logic in it.
You cannot return in the callback and need to synchronize it!
We recommend that you do not synchronize the callback function. You 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 codeThe Code is as follows:
Function GetJson (DataSourceName, callback ){
$. Ajax ({
Type: "post ",
Url: "Ajax/AjaxData. ashx? MethodName = "+ CENAME,
ContentType: "application/json ;",
Data: "",
DataType: "json ",
Success: function (Result ){
JsonData = Result;
Callback (JsonData)
},
Error: function (result ){
Alert ("An error occurred while obtaining the information List ");
Window. close ();
}
});
// Return JsonData;
}