XMLHttpRequest.setRequestHeader()

翻譯不完整。 請幫助我們翻譯這篇文章!

XMLHttpRequest 物件中的 setRequestHeader() 方法用來設定 HTTP 的表頭請求。當使用 setRequestHeader() 的時候,必須在 open() 之後呼叫,同時也必須在 send() 之前呼叫。如果這個方法被呼叫了許多次,且設定的表頭是一樣的,那所有設定的值會被合併成一個單一的表頭請求。

在第一次呼叫 setRequestHeader() 之後的每一次的呼叫,都會把給定的文字附加在已存在的表頭內容之後。

If no Accept header has been set using this, an Accept header with the type "*/*" is sent with the request when send() is called.

基於安全的理由,有些表頭只有使用者代理器可以使用。這些表頭包含了: forbidden header names  和 forbidden response header names.

Note: For your custom fields, you may encounter a "not allowed by Access-Control-Allow-Headers in preflight response" exception when you send requests across domains. In this situation, you need to set up the Access-Control-Allow-Headers in your response header at server side.

語法

XMLHttpRequest.setRequestHeader(header, value)

參數

header
想要設定所屬值的表頭名稱。
value
用來設定表頭本身的值。

回傳值

未定義

規範

Specification Status Comment
XMLHttpRequest
The definition of 'setRequestHeader()' in that specification.
Living Standard WHATWG living standard

瀏覽器相容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
setRequestHeaderChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5Opera Full support 8Safari Full support 1.2WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

Full support  
Full support

參見