如何以编程方式设置HTTP * Request *标头(例如,通过<a> -tag, <link/> -tag, or by JavaScript)?

时间:2016-01-29 15:05:04

标签: html ajax http-headers

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 e.g. 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!

2 个答案:

答案 0 :(得分:1)

XMLHTTPResponse setRequestHeader()方法可用于为AJAX请求设置HTTP请求标头。各种JavaScript库以不同的方式公开此函数。例如,使用jQuery.ajax(),您可以设置settings参数的headers属性。

答案 1 :(得分:1)

在某些方面,您可以在html中影响请求标头,例如通过表单的enctype属性。我不相信有任何方法可以在html中实际指定实际的标头值 对于Ajax,您可以指定请求标头,但浏览器会阻止设置某些标头,我怀疑max-stale是其中之一。
此外,不仅浏览器发出http请求,还有大量应用程序可以执行,如果需要,任何人都可以轻松设置max-stale标头。