AJPFX關於VIM的經常使用快捷鍵

Ajax技術的核心是XMLHttpRequest對象(簡稱XHR),
var xhr = new XMLHttpRequest();
function createXHR(){
if (typeof XMLHttpRequest != "undefined"){
return new XMLHttpRequest();
} else if (typeof ActiveXObject != "undefined"){
if (typeof arguments.callee.activeXString != "string"){
var versions = [ "MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0",
"MSXML2.XMLHttp"],
i, len;
for (i=0,len=versions.length; i < len; i++){
try {
new ActiveXObject(versions[i]);
arguments.callee.activeXString = versions[i];
break;
} catch (ex){
//跳過
}
}
}
return new ActiveXObject(arguments.callee.activeXString);
} else {
throw new Error("No XHR object available.");
}
}
2.xhr.open("get", "example.txt", false);
xhr.send(null);
3.responseText:做爲響應主體被返回的文本。
responseXML:若是響應的內容類是"text/xml"或"application/xml",這個屬性中將保 存包含着響應數據的XML DOM 文檔。
4.
0:未初始化。還沒有調用open()方法。
1:啓動。已經調用open()方法,但還沒有調用send()方法。
2:發送。已經調用send()方法,但還沒有接收到響應。
3:接收。已經接收到部分響應數據。
4:完成。已經接收到所有響應數據,並且已經能夠在客戶端使用了。
eg:
(get請求)var xhr = createXHR();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
alert(xhr.responseText);
} else {
alert("Request was unsuccessful: " + xhr.status);
}
}
};
xhr.open("get", "example.txt", true);
xhr.send(null);
(post請求)function submitData(){
var xhr = createXHR();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
alert(xhr.responseText);
} else {
alert("Request was unsuccessful: " + xhr.status);
}
}
};
xhr.open("post", "postexample.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var form = document.getElementById("user-info");
xhr.send(serialize(form));
}php

相關文章
相關標籤/搜索