Use $.post () to return the data of a function outside of the $.post () function

Source: Internet
Author: User

Originally, my idea was to take $.post () return function data out of the operation, searched the internet for a long time, said to be the $.post () method to synchronize, and the $.post () returned data assigned to a global variable method.

function GetData (pagecurrent)
{

var result;

$.ajax ({
URL: "/ajax/signmain.ashx",
async:true,//Change to synchronous mode
Type: "POST",
Data: {"pagecurrent": Pagecurrent},
Success:function (data) {
data = eval (' (' + data + ') ');
Result=data;

alert (result);
}

alert (result);
});


}

The first alert outputs the data normally, and the second alert shows the undefined. Many posts say this method can take the return data of $.post () out, but I really do not use it (if you can be useful to guide under the next, greatly appreciated. ), I can only think of him another way.

function GetData (pagecurrent,callback)
{
$.ajax ({
URL: "/ajax/signmain.ashx",
async:true,/
Type: "POST",
Data: {"pagecurrent": Pagecurrent},
Success:function (data) {
data = eval (' (' + data + ') ');
Callback (Pagecurrent,data);
}
});

}

A callback function is called to call the external function in the return function of the $.post (), and when the external function is called, the data is passed as a parameter to the external function, so that the function outside the $.post () is obtained in a disguised way $.post ().

Use $.post () to return the data of a function outside of the $.post () function

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.