[Go] In-depth understanding of Ajax Series--header message

Source: Internet
Author: User

Each HTTP request and response comes with the appropriate header information, some of which are useful to developers. The XHR object provides a way to manipulate the header information. This article describes the header information for HTTP in detail

Default information

By default, the following header information is also sent at the same time that the XHR request is sent

Accept: The type of content the browser can handle Accept-charset: The character set accept-encoding the browser can display: Compression encoding accept-language the browser can handle: The language connection The browser is currently set: Type of connection between browser and server cookie: Any cookiehost of the current page setup: The domain of the page where the request is made User-agent: the user agent string for the browser Referer: URI of the page that made the request

Note The HTTP specification misspelled this header field, and to ensure consistency with the specification, it can only be will wrong (the correct spelling should be referrer)

Set Head

use   setRequestHeader ()   method can settings Custom the request header information . This method accept two parameters : and the value of the header field . To successfully send the request header information, you must call open () method after and call send () method before call setRequestHeader () method  

A common use of the setRequestHeader () method is to set content-type header information to the content type submitted by the form when a POST request is used

Xhr.open (' Post ', ' service.php ', true); Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded '); Xhr.send (' data=test123 ');

[note] Try to use a custom header field name, do not use the field name normally sent by the browser, or it may affect the server's response

Xhr.open (' Get ', ' test.php ', true); Xhr.setrequestheader (' MyHeader ', ' myvalue '); Xhr.send ();    

The browser cannot add a custom header field to the message after testing

Call the getResponseHeader () method of the Xhr object and pass in the header field name to get the corresponding response header information . Call the getallresponseheaders () method to get a long string containing all the header information

var xhr;if (window. XMLHttpRequest) {    xhr = new XMLHttpRequest ();} else{    xhr = new ActiveXObject (' Microsoft.XMLHTTP ');} Asynchronous Accept Response Xhr.onreadystatechange = function () {    if (xhr.readystate = = 4) {        if (xhr.status = =) {/             *             Date : Wed, 14:00:21 GMT            server:apache/2.4.9 (Win32) php/5.5.12            connection:keep-alive            x-powered-by: php/5.5.12            content-length:1134            keep-alive:timeout=5, max=99            content-type:text/html              * *            Console.log (Xhr.getallresponseheaders ());            Console.log (Xhr.getresponseheader (' keep-alive '));//timeout=5, max=99        }else{            alert (' ERROR: ' + xhr.status ') ;        }    }} Send Request Xhr.open (' Get ', ' test.php ', true); Xhr.send ();    

In PHP, you can call the Apache_request_headers () method to get the header information of the request message

/*array (size=8)  ' Host ' = = String ' 127.0.0.1 ' (length=9)  ' Connection ' + = String ' keep-alive ' (length=10) c2/> ' upgrade-insecure-requests ' + string ' 1 ' (length=1)  ' user-agent ' = ' + String ' mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/55.0.2883.87 safari/537.36 ' (length=109)  ' Accept ' + = string ' text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 ' (length=74)  ' Referer ' = String ' http://127.0.0.1/box.html ' (length=25)  ' accept-encoding ' = = String ' gzip, deflate, SDCH, BR ' (length=23)  ' accept-language ' = String ' zh-cn,zh;q=0.8,en;q=0.6 ' (length=23) */var_dump (Apache_request_headers (

[Go] In-depth understanding of Ajax Series--header message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.