Home > Web Front-end > JS Tutorial > body text

The correct way to pass parameters to ajax callback function

不言
Release: 2018-07-02 16:44:49
Original
3087 people have browsed it

This article mainly introduces the correct method of passing parameters of ajax callback function, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

The correct method of passing parameters of ajax callback function. Many friends are used to writing incorrectly. Here is a brief summary.

Attribute methods can take parameters:

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
Copy after login

However, although the above is a reference directly defined in the function signature, if you do not call o.show('param') yourself, but When it is passed in through other function callbacks, it may not work because others may not pass in this parameter to you when calling this method. For example, when using ajax

request.onreadystatechange=function(param ){...}
or
request.onreadystatechange=callBack;function callBack(param){...}

is not easy to use, because at this time ajax is not given at all You pass the param parameter. The correct way is:

//request.onreadystatechange = orgEval;//错误作法 

//request.onreadystatechange = function (request, pOrgName) {//错误作法 
// orgEval(request, pOrgName); 
//}; 

//... 
request.onreadystatechange = function () {//正确作法 
orgEval(request, pOrgName);//在匿名函数内调用回调实现,并直接传入参数,这里用到了JavaScript的闭包性质 
}; 
//... 

function orgEval(req, orgName){ 
//... 
}
Copy after login

In this way, the function is implemented by calling the callback in the anonymous function, and the parameters are passed in directly.

ajax passes parameters to the callback function of onreadystatechange

I started to learn ajax in the past few days, and when I was doing a test page, I had a callback function that passed parameters to XMLHttpRequest.onreadystatechange this demand. After searching Baidu, I found that there are many people talking about this. The method found is probably like this:

xmlHttp.onreadystatechange=function(){callback(a,b);};
Copy after login

The two parameters a and b are passed.

Later I discovered a method myself. There must be many people who know the method, but I couldn’t find it on Baidu, so I will write it down here and promote it.

xmlHttp.a=a;
xmlHttp.b=b;
xmlHttp.onreadystatechange=callback;
.
.
function callback()
{
    if(this.readyState==4)
    {
        a=this.a;
        b=this.b;
    .
    }
}
Copy after login

That is, add two attributes to the xmlHttp object, and directly use this to call those two attributes in the callback function.

In addition, let me talk about my own experience. It is best to write xmlHttp as a global variable. At the beginning, I found that some requests did not achieve the purpose. Later I discovered that xmlHttp was written in a function. When that function finished running, the xmlHttp life cycle also ended. In this way, some requests are implemented before the end of the life cycle, and some requests are gone.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Introduction to Ajax get and post requests

About the method of ajax traversing xml documents

The above is the detailed content of The correct way to pass parameters to ajax callback function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template