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
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){ //... }
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);};
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; . } }
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!