关于ajax对象一些常用属性、事件和方法大小写比较常见的问题总

2019-09-14 07:02:40刘景俊

3)为post提交时,一定不要忘记在调用open方法后,再调用setRequestHeader方法设置content-type为application/x-www-form-urlencoded

4) 在使用responseText,responseXML和responseBody【IE only】,status属性时,需要在readyState==4,status状态==200【在线测试】或者status状态==0【本地测试】时再使用

JavaScript code

 var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("microsft.xmlhttp");
   xhr.open('get', 'index.html', true);
   xhr.onreadystatechange = function () {
     if (4 == xhr.readyState) {
       if (200 == xhr.status || 0 == xhr.status) {
         //=========正常返回后的处理代码
       }
       else alert('动态页出问题了~~');
     }
   }
   xhr.send(null);

5)如果为异步执行时,需要添加状态转换函数,然后在readyState位4时使用responseText或者responseXML属性。
如果为同步执行时,可以在send完后,直接使用responseText或者responseXML属性,不需要添加onreadystatechange状态转换函数了。不过为同步时如果网速慢容易造成浏览器假死,用户体验不好。

6)最简单的~~hoho~~改用jquery框架吧。