ajax

2017-07-23  本文已影响0人  西木木亢
var xhr = new XMLHttpRequest();
xhr.onreadyStateChange(function(){
  if(xhr.readyState==4){
    if(xhr.status ==200 || xhr.status ==304){
      alert(xhr.responseText);
    }else{
      alert("Request was unsuccessful : " + xhr.status);
     }
   }
})
//用open()方法并不会真正发送请求,而只是启动一个请求以备发送。
xhr.open("get","example.txt",true); //true:是否异步发送请求
//如果不需要通过请求主体发送数据,则必须传入null,因为这个参数对有些浏览器来说是必需的。
//调用send()之后,请求就会被分派到服务器。
xhr.send(null);

readyState

上一篇 下一篇

猜你喜欢

热点阅读