Ajax callback function parameters pass the correct method _ajax related

Source: Internet
Author: User
Tags anonymous
Property methods can be with parameters:
Copy Code code as follows:

function CLASSX (name) {
THIS.name = name;
ClassX.prototype.show = function (param) {
Alert (THIS.name + "" + param);
};
}
var o = new Classx ("name");
O.show ("param");//name param


However, the above is not necessarily useful, although it is defined directly in the function signature, if it is not called o.show (' param '), but through other function callbacks, because it is not necessarily necessary for others to call this method, such as when using Ajax

Request.onreadystatechange=function (param) {...}

Or

Request.onreadystatechange=callback;function callBack (param) {...}

is not good, because Ajax at this time does not give you the Param parameters, the correct way to form:
Copy Code code as follows:

Request.onreadystatechange = orgeval;//Wrong approach

Request.onreadystatechange = function (Request, porgname) {//Wrong approach
Orgeval (Request, porgname);
//};

//...
Request.onreadystatechange = function () {//correct approach
Orgeval (Request, Porgname)//Call the callback implementation within the anonymous function and pass directly to the parameter, which uses the closure nature of JavaScript
};
//...

function Orgeval (req, OrgName) {
//...
}

This is done by calling the callback implementation function within the anonymous function and passing in the argument directly.

Ajax pass parameter to onreadystatechange callback function

These days began to learn Ajax, in which they do the test page with the pass parameters to the Xmlhttprequest.onreadystatechange callback function this demand. If Baidu, found that there are a lot of people. The search method is probably this:

Xmlhttp.onreadystatechange=function () {callback (a,b);};

The two parameters of A and B are passed.

Later I found a way, the way to know a lot of people, but I did not search on Baidu, here is written to promote the next slightly.

Copy Code code as follows:

Xmlhttp.a=a;
Xmlhttp.b=b;
Xmlhttp.onreadystatechange=callback;
.
.
function callback ()
{
if (this.readystate==4)
{
A=THIS.A;
b=this.b;
.
}
}

That is, add two attributes to the XMLHTTP object and call the two properties in the callback function directly with this.

In addition to say a little bit of their own experience, XMLHTTP best to write global variables, I initially found that some of the requests did not achieve the goal. It was later found that the XMLHTTP was written in a function, and when that function ran out, the XMLHTTP life cycle ended. Some of these requests are implemented before the end of the lifecycle, and some requests are gone.

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.