XMLHttpRequest 简称 XHR ,使用 XHR 可以异步的和服务器进行交互,而无需刷新页面就可以刷新局部内容,在AJAX编程中,XMLHttpRequest 被大量使用。


var xhr = new XMLHttpRequest();// 1.创建xhr对象
xhr.timeout = 3000;// 设置超时时间

xhr.open("get","https://i.alapi.cn/hitokoto",true); //2.设置访问方式,url,是否异步

//注册相应事件
xhr.onreadystatechange= function(e){
 if(this.readyState == 4 && this.status == 200){
   console.log(this.responseText);
}
};
xhr.send(null);//发送数据

更多资料参考:

  1. https://segmentfault.com/a/1190000004322487
    2.https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
Last modification:September 18th, 2019 at 09:32 am
如果觉得我的文章对你有用,请随意赞赏