javascript之Ajax擷取和設定標題

來源:互聯網
上載者:User

標籤:doc   oct   handle   金融   func   服務   上市   .net   nts   

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擷取和設定標題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.