msforest / notebook

好记性不如烂笔头,记录知识的点点滴滴。
https://github.com/msforest/notebook/wiki
0 stars 0 forks source link

ajax(异步js和xml) #13

Open msforest opened 7 years ago

msforest commented 7 years ago

AJAX全称: Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)

1. 创建XMLHttpRequest对象

var xhr;
if (window.XMLHttpRequest)
{
    //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    xhr=new XMLHttpRequest();
}
else
{
    // IE6, IE5 浏览器执行代码
    xhr=new ActiveXObject("Microsoft.XMLHTTP");
}

2. 请求

xhr.open("GET","ajax_info.txt",true);  //arg1:请求类型;arg2:请求url;arg3:是否异步
xhr.send();  //将请求数据发送到服务器,请求类型为post时,需上传所传送参数

2.1 请求类型

2.2 是否异步

3. 响应

响应的数据会自动填充到xhr对象的属性:responseText/responseXML