开发者

Small questions for AJAX onreadystatechange

开发者 https://www.devze.com 2022-12-30 06:52 出处:网络
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开发
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

精彩评论

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