異步js和xml,頁面不整個重新整理的情況下發送http請求和處理回應
原理
xmlhttprequest對象,是一個js對象
在進階浏覽器,直接new xmlhttprequest建立,在老版本的ie上用activex對象建立new
activexobject("microsoft.xmlhttp")
發送請求的方法
方法
描述
open(method,url,async)
規定請求的類型、url 以及是否異步處理請求。
method:請求的類型;get 或 post
url:檔案在伺服器上的位置
async:true(異步)或 false(同步)
send(string)
将請求發送到伺服器。
string:僅用于 post 請求
setrequestheader(header,value)
向請求添加 http 頭。
header: 規定頭的名稱
value: 規定頭的值
像表單一樣送出資料
returns all the response headers as a string,
or <code>null</code> if no response has been
received. note: for multipart requests, this returns
the headers from the current part of the request, not from
the original channel.
returns the string containing the text of the specified header,
or <code>null</code> if either the response has not yet been received
or the header doesn‘t exist in the response.
屬性
responsetext
獲得字元串形式的響應資料。
responsexml
獲得 xml 形式的響應資料。
onreadystatechange
存儲函數(或函數名),每當 readystate 屬性改變時,就會調用該函數。
readystate
存有 xmlhttprequest 的狀态。從 0 到 4 發生變化。
0: 請求未初始化
1: 伺服器連接配接已建立
2: 請求已接收
3: 請求進行中
4: 請求已完成,且響應已就緒
status
200: "ok"
404: 未找到頁面