XMLHttpRequest是一个前端获取服务端数据的API对象。
var request = new XMLHttpRequest();
request.onload = function() {
if(this.status == 200 &&
this.responseXML != null &&
this.responseXML.getElementById('test').textContent) {
// success
function(data) {
// processing data
}
} else {
// handle error
}
}
request.open("GET", "xxxUrl");
request.send();
示例中实例化了一个XMLHttpRequest对象,通过为该对象绑定onload回调函数处理从xxxUrl返回来的数据。
XMLHttpRequest支持的事件主要有:
open函数:
send函数:
发送请求。如果该请求是异步模式,该方法会立刻返回。如果请求是同步模式,则直到请求的响应完全接受以后,该方法才会返回。