DIV CSS 佈局教程網

用 JS 透過 AJAX 取得指定 URL 回應標頭中特定欄位的方法
編輯:關於JavaScript     

除了取得回應本文,AJAX 也能讀取伺服器回應的標頭資訊。本文示範如何取得 ajax_info.txt 的 Last-Modified(最後修改時間)欄位。

 <!DOCTYPE html> <html> <head> <script> function loadXMLDoc(url) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById('p1').innerHTML="Last modified: " + xmlhttp.getResponseHeader('Last-Modified'); } } xmlhttp.open("GET",url,true); xmlhttp.send(); } </script> </head> <body> <p id="p1">The getResponseHeader() function is used to return specific header information from a resource, like length, server-type, content-type, last-modified, etc.</p> <button onclick="loadXMLDoc('ajax_info.txt')">Get "Last-Modified" information</button> </body> </html>

關鍵在於 getResponseHeader() 方法:它能取得資源回應標頭中的特定欄位,如長度、伺服器類型、內容類型、最後修改時間等。把參數換成其他標頭名稱,就能取得對應資訊。希望本文對你的 JavaScript 開發有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved