xmlhttp.onreadystatechange=function() {
So this says onreadystatechange
, invoke function(). Can I put parameters in function()
? Second question, what does it mean when someone writes, xmlhttp.onr开发者_如何学Pythoneadystatechange=statechanged
? Does that mean it will always be true or something?
You can't use a parameter since
onreadystatechange
has no parameters to give you. What parameter are you expecting? It is simply a hook for handling the response. What you do have available is thexmlhttp.readyState
which tells you if the response is ready or not,xmlhttp.status
- the http status code (i.e. 200), andxmlhttp.responseText
- the response itself.No - that means you are assigning a variable reference (a function) to
onreadystatechange
.
I would strongly suggest using a JS framework (such as jQuery) to do AJAX calls - it will abstract away the low-level details you are asking about. If you must use native JS AJAX calls - read this tutorial.
You can pass parameters with a wrapper function:
var func = function(p1, p2) {/*...*/};
xmlhttp.onreadystatechange = function() {
func(foo, bar);
};
精彩评论