AJAX即Asynchronous Javascript And Xml,異步javascript和xml,主要用戶在不刷新頁面的情況下與服務器數據交互。
Ajax主要用到的對象為XMLHttpRequest(在IE5、IE6中為ActiveXObject)
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open(method,url,async);
xmlhttp.setRequestHeader()
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState ==4&&xmlhttp.status==200){
xmlhttp.getResponseHeader(string)
xmlhttp.responseXML
xmlhttp.responseText
}
}
xmlhttp.send(string);
---------------------------------------------------------------------------------------------------------
&.ajax({
type:"GET",
url:"http://",
success:function(msg){},
error:function(xmlhttprequest,textstatus,errorThrow){},
statusCode:{
404:function(){}
},
complete:function(xmlhttprequest,textstatus){}
});
正常情況下,ajax向指定url以指定方法發送數據,服務器處理完之后返回狀態200,且content-type:text/plain的數據,客戶端接收到返回的數據進行處理。如果發送ajax數據時session已過期,服務器要求重定向到登陸頁面,此時前台並不能獲取到302的狀態碼,XMLHttpRequest對象會直接向后台發起重定向請求,然后返回狀態200,content-type:text/html;msg以及responseText為登陸頁面的html。
在complete中判斷如果返回的content-type為text/html則顯示該頁面
document.write(xmlhttprequest.responseText);
document.close();(必須的)
如果ajax請求是在嵌套頁面中發送的,登陸頁面要顯示在最頂層的頁面中,可以在登錄頁面中調用
if (top.location != window.location) {
top.location = window.location;
}
防止頁面嵌套。
