The correct way to pass parameters to ajax callback function
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The writing methods of java callback function are: 1. Interface callback, define an interface, which contains a callback method, use the interface as a parameter where the callback needs to be triggered, and call the callback method at the appropriate time; 2. Anonymous inner class callback , you can use anonymous inner classes to implement callback functions to avoid creating additional implementation classes; 3. Lambda expression callbacks. In Java 8 and above, you can use Lambda expressions to simplify the writing of callback functions.

Introduction to the basic writing and usage of Java callback functions: In Java programming, the callback function is a common programming pattern. Through the callback function, a method can be passed as a parameter to another method, thereby achieving indirect call of the method. The use of callback functions is very common in scenarios such as event-driven, asynchronous programming and interface implementation. This article will introduce the basic writing and usage of Java callback functions, and provide specific code examples. 1. Definition of callback function A callback function is a special function that can be used as a parameter

Vue component communication: using callback functions for component communication In Vue applications, sometimes we need to let different components communicate with each other so that they can share information and collaborate with each other. Vue provides a variety of ways to implement communication between components, one of the common ways is to use callback functions. A callback function is a mechanism in which a function is passed as an argument to another function and is called when a specific event occurs. In Vue, we can use callback functions to implement communication between components, so that a component can

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

Application of Java callback function in event-driven programming Introduction to callback function A callback function is a function that is called after an event or operation occurs. It is commonly used in event-driven programming, where the program blocks while waiting for an event to occur. When the event occurs, the callback function is called and the program can continue execution. In Java, callback functions can be implemented through interfaces or anonymous inner classes. An interface is a mechanism for defining function signatures that allows one class to implement another class's

Callback functions are one of the concepts that every front-end programmer should know. Callbacks can be used in arrays, timer functions, promises, and event handling. This article will explain the concept of callback functions and help you distinguish between two types of callbacks: synchronous and asynchronous.

Analysis of common callback function application scenarios in Python requires specific code examples. A callback function refers to passing a function as a parameter to another function in programming, and executing this parameter function when a specific event occurs. Callback functions are widely used in asynchronous programming, event processing, GUI programming and other fields. This article will analyze common callback function application scenarios in Python and give relevant specific code examples. Asynchronous Programming In asynchronous programming, callback functions are often used to handle the results of asynchronous tasks. When it is necessary to execute a consumption

Function pointers and callback functions are both tools for implementing the callback mechanism. Function pointers are created at compile time and cannot be modified and need to be called explicitly; callback functions are created at runtime and can be dynamically bound to different functions and automatically called by the callback function. Therefore, function pointers are suitable for static callbacks, while callback functions are suitable for dynamic callbacks.
