开发者

Small questions for AJAX onreadystatechange

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?


  1. 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 the xmlhttp.readyState which tells you if the response is ready or not, xmlhttp.status - the http status code (i.e. 200), and xmlhttp.responseText - the response itself.

  2. 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);
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜