
AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。
在不刷新整个页面的情况下,与服务器交换数据并更新部分网页的内容
XMLHttpRequest是AJAX的基础
var xhr=new XMLHttpRequest();
內建
if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
else{
xhr=new ActiveObject(“Microsoft.XMLHttp”)
}
发起请求
open(method,url,async)
send(string)
类型:GET POST
async:true 异步 false 同步
xhr.open(“GET”,”/ajax/ajaxWeb”,true);
xhr.send();
xhr.open(“POST”,”/ajax/ajaxWeb”,true);
xhr.send(“un=张三&pass=123456”);
XHR的响应
responseText 获得字符串形式的响应数据
responseXML 获得XML形式的响应数据
XHR readyState
onreadystatechange
readyState
0
1
2
3
4
status
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&status==200){
document.getElementById(“myDiv”).innerHTML=xhr.responseText;
}
}
jQuery提供了多个与Ajax相关的方法
GET POST
文本、HTML XML JSON
加载
load()
load方法有三个参数
load(URL,data,callback)
$(“#div1”).load(“demo.txt”,function(responseTxt,statusTxt,xhr){
})
get() / post()
$.get(URL,callback);
$.get(“/ajaxJqueryWeb/ajaxTest”,function(data,status){
});
$.post(URL,data,callback);
$.post(“/ajaxJqueryWeb/ajaxTest”,
{name:”zhangsan”,
age:35
},function(){
})




近期评论