ajax|response
Ajax Hacks-hack9 深入瞭解HTTP Response
HTTP回應標頭是描述性的資訊,符合HTTP 1.1 協議規範,web伺服器向請求端發送精確的頁面或資料。如果你已經使用XMLHttpRequest對象編過程(在本章的前面討論過),你應該知道request.status屬性是從伺服器返回的響應狀態代碼。它是用來檢測HTTP響應狀態的重要值。
裝態值包括200(請求成功)、404(請求的URL不存在,或頁面不存在)500(伺服器內部錯誤)。
然而,你還想看到更多的其他優選請求的回應標頭資訊,例如與響應相關的web伺服器軟體類型(伺服器要求標頭),或者響應的content類型(Content-Type)。本hack請求使用者輸入的一個URL。當使用者點擊輸入框以外的部分時,瀏覽器會顯示該URL的回應標頭資訊。作為一個ajax應用,頁面是不會出現重新整理情況的。
請求對象方法只返回可用的回應標頭的一個子集,包括Content-Type, Date, Server,和Content-Length.
HTML代碼如下:
“http://www.w3.org/TR/1999/REC-html401–19991224/strict.dtd”>
Find out the HTTP response headers when you "GET" a Web page
javascript:void%200>
Enter a URL:
::press tab when finished editing the
field::
Figure 1-13 shows the page in the Safari browser.
Figure 1-13. Scoping the response
程式預先使用http://www.parkerriver.com/s/sender?name=guest來填充該text。
當使用者輸入URL以後點擊Tab鍵,或點擊輸入框外的部分時,輸入框的onblur事件被激發。事件處理函數是getAllHeaders,該函數傳遞使用者輸入的RUL進入request對象。request對象向此URL發送請求,並向web頁面返回的回應標頭。
下面的代碼是檔案hack7.js。顯示代碼以後,將解釋一下如何顯示伺服器回應標頭資訊。其餘的可以參考本章其他hack。
var request;
var urlFragment=“http://www.parkerriver.com/s/sender?name=guest”;
function getAllHeaders(url){
httpRequest(“GET”,url,true);
}
//function for XMLHttpRequest onreadystatechange event handler function handleResponse( ){ try{ if(request.readyState == 4){ if(request.status == 200){ /* All headers received as a single string */ var headers = request.getAllResponseHeaders( ); var div = document.getElementById(“msgDisplay”; div.className=“header”; div.innerHTML=”
"+headers+"
"; } else { //request.status is 503 if the application isn‘t available; //500 if the application has a bug alert(request.status); alert(“A problem occurred with communicating between ”+ “the XMLHttpRequest object and the server program.”; } }//end outer if } catch (err) { alert(“It does not appear that the server is ”+ “available for this application. Please”+ “ try again very soon. \\nError: ”+err.message);
}
}
/* Initialize a request object that is already constructed */
function initReq(reqType,url,bool){
try{
/* Specify the function that will handle the HTTP response */
request.onreadystatechange=handleResponse;
request.open(reqType,url,bool);
request.send(null);
} catch (errv) {
alert(
“The application cannot contact the server at the moment. ”+
“Please try again in a few seconds.” );
}
}
/* Wrapper function for constructing a request object.
Parameters:
reqType: The HTTP request type, such as GET or POST.
url: The URL of the server program.
asynch: Whether to send the request asynchronously or not. */
function httpRequest(reqType,url,asynch){
//Mozilla-based browsers
if(window.XMLHttpRequest){
request = new XMLHttpRequest( );
} else if (window.ActiveXObject){
request=new ActiveXObject(“Msxml2.XMLHTTP”;
if (! request){
request=new ActiveXObject(“Microsoft.XMLHTTP”;
}
}
//the request could still be null if neither ActiveXObject
//initialization succeeded
if(request){
initReq(reqType,url,asynch);
} else {
alert(“Your browser does not permit the use of all ”+
“of this application‘s features!”;
}
}
令人感興趣的部分是handleResponse函數。該函數調用request對象的getAllResponseHeaders方法,該方法返回所有的可用的回應標頭資訊,預格式為string。開發人員更喜歡這些值作為JSON格式返回,而不是作為一個單一的字串返回。
為取得一個頭資訊,也可以使用request.getResponseHeader方法。例如request.getResponseHeader("Content-Type")可以得到回應標頭的Content類型:
接下來代碼將取得div元素,並在其中顯示頭資訊:
if(request.status == 200){ /* All headers received as a single string */ var headers = request.getAllResponseHeaders( ); var div = document.getElementById(“msgDisplay”; div.className=“header”; div.innerHTML=”
"+headers+"
"; }...
為能向資訊顯示提供CSS格式,代碼設定了div的className屬性,這個類已經預先定義了,代碼如下:
div.header{ border: thin solid black; padding: 10%;
font-size: 0.9em; background-color: yellow}
span.message { font-size: 0.8em; }
以這樣的方式,代碼會動態地將一個div連向一個特定的CSS類,這個類是單獨定義的。這種策略協助DOM編程者描述區分。最後div的innerHTML 屬性被設定進返回頭值裡邊。
<