简体   繁体   中英

How set HTTP *Request* header programmatically (e.g. by <a> -tag, <link> -tag, or by JavaScript)?

This question is about HTTP Request headers -- not HTTP Response headers.

Is there any way to programmatically make User Agents add or change any HTTP Request Header eg by means of HTML, JavaScript, for links or resources from a web page. There exists some HTTP Request Headers that one would think should be able to be controlled programmatically, for example max-stale .

(One could for example imagine that some optional attribute for HTML-elements <a> , <link> , <img> <script> etc, could exist that would cause the User Agent to add/change a HTTP request header in its request to the server, but I have not found such an attribute. Similarly, one could think that there should exist some mechanism to control HTTP Request Headers when AJAX is used.)

Turning around the question ...: Why does the HTTP Request Header max-stale exist at all if it cannot be controlled?

Thanks!

The XMLHTTPResponse setRequestHeader() method can be used to set HTTP request headers for an AJAX request. Various JavaScript libraries expose this function in different ways. For example, with jQuery.ajax() you would set the headers property of the settings parameter.

There are some ways in which you can affect request headers in html, eg via a form's enctype attribute. I do not believe there is any way to actually specify the actual header values in html.
For Ajax you can specify request headers, though the browser will block certain headers from being set, I doubt max-stale is one of them.
Also not only browsers make http request, there are a whole host of applications that do and any of them could easily set the max-stale header if they wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM