XMLHttpRequest.setRequestHeader( ): add a HTTP request header to the request


void setRequestHeader(Stringname, String value)



The name of the header to be set. This argument should not contain spaces, colons, linefeeds, or newlines.


The value for the header. This argument should not contain linefeeds or newlines.


setRequestHeader( ) specifies an HTTP request header that should be included in the request issued by a subsequent call to send( ). This method may be called only when readyState is 1—i.e., after a call to open( ) but before a call to send( ).

If a header with the specified name has already been specified, the new value for that header is the previously specified value, plus a comma, a space, and the value specified in this call.

If the call to open( ) specifies authorization credentials, XMLHttpRequest automatically sends an appropriate Authorization request header. You can append to this header with setRequestHeader( ), however. Similarly, if the web browser has stored cookies associated with the URL passed to open( ), appropriate Cookie or Cookie2 headers are automatically included with the request. You can append additional cookies to these headers by calling setRequestHeader( ). XMLHttpRequest may also provide a default value for the User-Agent header. If it does this, any value you specify for that header is appended to the default value.

Some request headers are automatically set by the XMLHttpRequest for conformance to the HTTP ...

Get JavaScript: The Definitive Guide, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.