2015年10月19日
Ajax
创建 XMLHttpRequest 对象
variable=new XMLHttpRequest();
Ajax调用示例
//创建XHR对象
var xhr=new XMLHttpRequest();
//处理返还数据
xhr.onreadystatechange=function(callback){
if(xhr.readyState==4){
if((xhr.status>=200&&xhr.status<300)||xhr.status==304){
callback(xhr.responseText);
}else{
alert("Request was unsuccessful:"+xhr.status);
}
}
}
//发送请求
xhr.open('get','example.json',true);
xhr.setRequestHeader('myHeader','myValue');
xhr.send(null);
xhr.open(method,url[,async=true);
method一般有get
、post
等方法。 url[ :请求资源相对文档路径。 async :是一个布尔值。
xhr.setRequestHeader('myHeader','myValue');
XMLHttpRequest 对象的三个重要的属性
属性
描述
onreadystatechange
存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。
readyState
存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。
0: 请求未初始化
1: 服务器连接已建立
2: 请求已接收
3: 请求处理中
4: 请求已完成,且响应已就绪
status
200: "OK"
404: 未找到页面
慕课
金牌用户策略
信息平衡策略
资源聚焦策略
Last updated
Was this helpful?