In-depth understanding of the third chapter of the AJAX series-header information

Source: Internet
Author: User

Previous words

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 content type that the browser can handle accept-charset: The character set that the browser can display is accept-encoding: The compression code that the browser can handle is accept- Language: Browser currently set language connection: type of connection between browser and server cookie: Any cookiehost that is set on the current page:  The domain in which the requested page is locateduser-agent: browser's user agent string Referer: URI of the page making 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 the setRequestHeader () method to set the custom request header information. This method takes two parameters: the name of the header field, the value of the header field. To successfully send the request header information, the setRequestHeader () method must be called after the Open () method is called and before the Send () method is called

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

varxhr;if(window. XMLHttpRequest) {XHR=NewXMLHttpRequest ();}Else{XHR=NewActiveXObject (' Microsoft.XMLHTTP ');}//Accept Responses AsynchronouslyXhr.onreadystatechange =function(){    if(Xhr.readystate = = 4){        if(Xhr.status = = 200){             /*date:wed, 14:00:21 GMT server:apache/2.4.9 (Win32) php/5.5.12 Connect Ion:keep-alive x-powered-by:php/5.5.12 content-length:1134 keep-alive:timeout=5, max= Content-type:text/html*/Console.log (Xhr.getallresponseheaders ()); Console.log (Xhr.getresponseheader (' keep-alive '));//timeout=5, max=99}Else{alert (' Error occurred: ' +xhr.status); }    }}//Send RequestXhr.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) ' Upgra De-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 ());

In-depth understanding of the third chapter of the AJAX series-header information

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.