Analysis of Ajax execution sequence flow and callback Problems

Source: Internet
Author: User

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 notCopy 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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.