Ajax execution Sequence Flow and callback problem Analysis _ Basics

Source: Internet
Author: User
A global variable var jsondata;
I have a way to handle Ajax here:
JScript Code:
Copy Code code 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 ("Get information list error");
Window.close ();
}
});
return jsondata;
}

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

function Drawdropdownlist (Sfieldrulemethod)
{
Getjson (Sfieldsourcename);
var B = jsondata;
}

So, why do I always get jsondata when I perform drawdropdownlist?
I interrupted the tracking of the following, found that the Drawdropdownlist method inside all of the implementation will not enter the Getjson method,
Is there any way to bring out the result data obtained in Getjson?
Not in
Copy Code code as follows:

Success:function (Result) {
Do something
},

I just want to get the data out, because Getjson is a common method and does not want to execute a single logic inside.
Can not return in the callback, and need to sync, on it!
Another kind of pass is not recommended synchronization, you need to add a function parameter to my function as a callback function, the results of the AJAX passed to the function, the following code details:
Copy Code code as follows:

function Getjson (datasourcename,callback) {
$.ajax ({
Type: Post)
URL: "Ajax/ajaxdata.ashx?" Methodname= "+ datasourcename,
ContentType:" Application/json; ",
Data:",
DataType: "JSON",
Succe Ss:function (Result) {
Jsondata = result;
Callback (Jsondata)
},
Error:function (result) {
Alert ("Get information list error");
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.