同步雖然簡單,但使用異步調(diào)用 才是我們真正常用的手段 。使用異步 調(diào)用的時(shí)候 需要觸發(fā)readystatechange事件,然后檢測readyState屬性即可。這個(gè)屬性有五個(gè)值 ,0 1 2 3 4 。
addEvent(document,'cllick',function(){
var xhr= createXHR();
xhr.onreadystatechange=function(){
//alert(xhr.readyState) // 1 2
if(xhr.readyState==4){
if(xhr.status == 200){
alert(xhr.responseText);
}
}else{
alert('獵取數(shù)據(jù)錯(cuò)誤,錯(cuò)誤代號:'+xhr.status+'錯(cuò)誤信息:'+xhr.statusText);
}
};
xhr.open('get','demo.php?rand='+Math.random(),true);
xhr.send(null);
// xhr.abort() //取消異步請求
});
GET POST請求
GET使用頻率高于POST,
在WEB程序上,GET一般是URL提交請求,比如:demo.php?name=lee&age=100
www.abc.com/list.php?page=2
list.php接收到page=2這個(gè)參數(shù)后再去請求數(shù)據(jù)庫
PS:如何理解句中的問號?
給你舉個(gè)栗子、
網(wǎng)站文章頁列表使用動(dòng)態(tài)方式展示
如何讓程序知道我要展示哪一頁
www.abc.com/list.php?page=2
list.php接收到page=2這個(gè)參數(shù)后再去請求數(shù)據(jù)庫
然后把數(shù)據(jù)庫返回的數(shù)據(jù)用html標(biāo)簽輸出
最初的網(wǎng)站都是動(dòng)態(tài)展示的,后來為了得到好的搜索引擎排名才出現(xiàn)了將動(dòng)態(tài)生成靜態(tài)的潮流
所謂動(dòng)態(tài)就是 一個(gè)動(dòng)態(tài)語音頁面通過傳入不同的參數(shù)就輸出不同的內(nèi)容
舉個(gè)不帶問題的動(dòng)態(tài)鏈接
www.00lw.com/index.php
織夢做的站 首頁調(diào)用了最新文章的情況下
如果采用靜態(tài)模式 需要每次生成首頁才能顯示最新增加的文章
使用動(dòng)態(tài)流量模式就不需要了 增加完新文章后 直接訪問就能顯示
因?yàn)閯?dòng)態(tài)頁面每次都去數(shù)據(jù)庫請求最新的文章
而靜態(tài)HTML是寫死的
PST一般是WEB表單提交 比如:
<form method="post"><input type ="text" name="name" value="lee">
HTTP頭信息
//在網(wǎng)絡(luò)那里查看
兩種頭信息:一種是響應(yīng)頭信->服務(wù)器返回的信息,客戶端可以獲取,但不可以設(shè)置 。一個(gè)是請求頭信息->是可以設(shè)置 但不可以獲取
//頭信息
addEvent(document,'cllick',function(){
var xhr= createXHR();
xhr.onreadystatechange=function(){
//alert(xhr.readyState) // 1 2
if(xhr.readyState==4){
if(xhr.status == 200){
// alert(xhr.getAllResponseHeaders()); //獲取全部響應(yīng)頭信息
alert(xhr.getResponseHeader('Content-Type')); //獲取意念響應(yīng)頭信息
}
}else{
alert('獵取數(shù)據(jù)錯(cuò)誤,錯(cuò)誤代號:'+xhr.status+'錯(cuò)誤信息:'+xhr.statusText);
}
};
xhr.open('get','demo.php?rand='+Math.random(),true);
xhr.setRequestHeader('myheader','lee');//設(shè)置請求頭信息,一般沒什么用,在POST提交請求有用
xhr.send(null);
// xhr.abort() //取消異步請求
});
GET請求
addEvent(document,'cllick',function(){
var xhr= createXHR();
xhr.onreadystatechange=function(){
//alert(xhr.readyState) // 1 2
if(xhr.readyState==4){
if(xhr.status == 200){
alert(xhr.responseText);
}
}else{
alert('獵取數(shù)據(jù)錯(cuò)誤,錯(cuò)誤代號:'+xhr.status+'錯(cuò)誤信息:'+xhr.statusText);
}
};
xhr.open('get','demo.php?rand='+Math.random(),true);
xhr.send(null);
// xhr.abort() //取消異步請求
});
//中文亂碼問題 AJAX返回的數(shù)據(jù)其實(shí)是UTF-8,最簡單的辦法是全部改為UTF-8;
//特殊字符 需要通過encodeURIComponent來編碼解決
addEvent(document,'cllick',function(){
var xhr= createXHR();
var url='demo.php?rand='+Math.random();
//alert(url);
url=params(url,'name',Le&e);
url=params(url,'age',100);
xhr.onreadystatechange=function(){
//alert(xhr.readyState) // 1 2
if(xhr.readyState==4){
if(xhr.status == 200){
alert(xhr.responseText);
}
}else{
alert('獵取數(shù)據(jù)錯(cuò)誤,錯(cuò)誤代號:'+xhr.status+'錯(cuò)誤信息:'+xhr.statusText);
}
};
xhr.open('get','demo.php?rand='+Math.random(),true);
xhr.send(null);
// xhr.abort() //取消異步請求
});
function params(url,name,value){
url+=url.indexOf('?')==-1?'?':'&';
url += encodeURIComponent(name) +'='+encodeURIComponent(value);
return url;
}
一般來說,向服務(wù)器發(fā)送OST請求明白股份解析機(jī)制的原因 ,需要進(jìn)行特別的處理。因?yàn)镻OST請求和EB表單提交是不同的,需要使用XHR來模仿表單提交 。
//PS 使用XHR來模仿表單提交 有三步
xhr.open('post',url,true); //第一步 改為POST
xhr.steRequest('Content-Type','application/x-www-form-urlencoded'); //第三步 模擬表單提交
xhr.send('name=lee&age=100'); //第二步 將名值對放入send方法