標籤:htm5 前端 網頁設計 javascript ajax
XMLHttpRequest對象中與標題有關的方法:
setRequestHeader(<header>,<value>)——用指定值設定標題;
getResponseHeader(<header>)——擷取指定標題的值;
getAllResponseHeaders()——以單個字串形式擷取所有標題;
覆蓋請求的HTTP方法,如下代碼:可規避防火牆只允許GET和POST請求通過的限制;
httpRequest.setRequestHeader("X-HTTP-Method-Override","DELETE");
禁用內容緩衝:
httpRequest.setRequestHeader("Cache-Control","no-cache");
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8"> <title>上海遠地資產管理有限公司</title> <meta name="author" content="jason"/> <meta name="description" content="上海遠地資產管理有限公司(簡稱:遠地資產),是一家專業的互連網金融服務平台."/> <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon"/> <style type="text/css"> #allheaders,#ctheader{ border: medium solid black; padding: 2px; margin: 2px; } </style></head><body> <div> <button>Apples</button> <button>Cherries</button> <button>Bananas</button> </div> <div id="ctheader"></div> <div id="allheaders"></div> <div id="target"> 載入內容 </div> </table> <script> var buttons=document.getElementsByTagName("button"); for(var i=0;i<buttons.length;i++){ buttons[i].onclick=handleButtonPress; } var httpRequest; function handleButtonPress(e){ httpRequest=new XMLHttpRequest(); httpRequest.onreadystatechange=handleResponse; httpRequest.open("GET", e.target.innerHTML+".html"); //規避防火牆的限制,使用X-HTTP-Method-Override標題來指定想要的HTTP方法,但形式上市再發送一個POST請求 //httpRequest.setRequestHeader("X-HTTP-Method-Override","DELETE"); //禁用內容緩衝 //httpRequest.setRequestHeader("Cache-Control","no-cache"); httpRequest.send(); } function handleResponse(){ if(httpRequest.readyState==2){ document.getElementById("allheaders").innerHTML=httpRequest.getAllResponseHeaders(); document.getElementById("ctheader").innerHTML=httpRequest.getResponseHeader("Content-Type"); }else if(httpRequest.readyState==4 && httpRequest.status==200){ document.getElementById("target").innerHTML= httpRequest.responseText; } } </script><pre name="code" class="html"></body></html>
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
javascript之Ajax擷取和設定標題