Переклад цієї статті ще триває.

Інтерфейс Headers  Fetch API дозволяє вам виконувати різні дії з  HTTP запитами і заголовками відповідей. Ці дії включають отримання налаштування, додавання і видалення . Об'єкт Headers має асоціативний список заголовків, який по замовчуванню пустий і містить ноль чи більше пар імен і значень. Ви можете додавати то цього списку, використовуючи метод типу append(). У всіх методах цього інтерфейсу імена заголовків знаходяться по  не чутливим до реєсту запитам 

З метою безпеки деякі заголовки можуть контролюватись тільки за допомогою user agent. Ці заголовки включають: forbidden header names  і forbidden response header names.

Об'єкт заголовку також має associated guard, що приймає значення: immutable, request, request-no-cors, response чи none. Це впливає на те чи меоди: set(), delete(), і append() змінюватимуть заголовок. Для більшої інформації дивіться Guard.

Ви можете отримати об'єкт Headers  через властивості Request.headers іResponse.headers, і створити новий об'єкт  Headers, використовуючи конструктор Headers.Headers().

Об'єкт, що імплементує Headers може напряму використовуватись в структурі for...of замістьentries(): for (var p of myHeaders)тотожний for (var p of myHeaders.entries()).

Нотатка: Ви можете знайти більше про доступні заголовки прочитавши довідку по HTTP headers.

Конструктор

Headers.Headers()
Створює новий об'єкт заголовку Headers.

Методи

Headers.append()
Додає нове значення до існуючого заголовка в об'єкті Headers, або додає заголовок якщо той не існує.
Headers.delete()
Видаляє заголовок з об'єкта Headers.
Headers.entries()
Повертає iterator, дозволяючи прогодити через всі пари ключ/значення, що знаходяться в об'єкті.
Headers.forEach()
Виконує вказану функцію для кожного елементу масива.
Headers.get()
Повертає ByteString послідовність всіх значеннях заголовків об'єкта Headers з заданим іменем.
Headers.has()
Повертає логічне значення чи має обєкт Headers певний заголовок
Headers.keys()
Повертає iterator, що дозволяє вам пройтись по всім ключам пар ключ/значення, що містяться в об'єкті.
Headers.set()
Задає нове значення існуючого заголовку всередині об'єкта Headers, або додає заголовок, якщо той не існтує.
Headers.values()
Повертає iterator, що дозволяє вам проходитись по всім значенням пар ключ/значення, які містяться в цьому об'єкті.

Нотатка:  Різниця між Headers.set() і Headers.append() в тому, що якщо о if the вказаний заголовок уже існує і допускає багато значень, Headers.set() перезаписуватиме існуючі значення новим., тоді як Headers.append() буде додавати нове значення в кінець набору значеньs. Дивіться їх сторінки для прикладу кода.

Нотатка: Всі методи Headers видаватимуть TypeError якщо ви спробуєте передати не Валідне ім'я заголовку. Операції зміни заголовку видаватимуть TypeError якщо заголовок  незмінний Guard. У випадку інших невдачі не видаватимуть помилка .

Нотатка: Коли значення Header повторюються вони автоматично сортуються в лексикографічному порядку і значення дубльованих заголовків комбінуються.

Застарілі методи

Headers.getAll()
Використовужться для повернення масиву всіх значень заголовка з заданим іменем в об'єкті Headers ; цей метод - видалений із специфікації і натомість тепер Headers.get() повертає всі значення замість одного.

Приклади

В наступномуIn the following фрагменті ми створили новий заголовок, використавши конструктор Headers() constructor, додали новий заголовок за допомогою append(), потім повернули значення цього заголовку, використовуючи get():

var myHeaders = new Headers();

myHeaders.append('Content-Type', 'text/xml');
myHeaders.get('Content-Type') // should return 'text/xml'

Те ж можна досягнути передаванням масиву масивів чи літералу об'єкта до конструктора:

var myHeaders = new Headers({
    'Content-Type': 'text/xml'
});

// or, using an array of arrays:
myHeaders = new Headers([
    ['Content-Type', 'text/xml']
]);

myHeaders.get('Content-Type') // should return 'text/xml'

Специфікація

Специфікація Статус Коментар
Fetch
The definition of 'Headers' in that specification.
Living Standard  

Підтримка браузерів

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
HeadersChrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support YesFirefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Full support 10.1WebView Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile ? Firefox Android Full support 44Opera Android Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No support NoSamsung Internet Android Full support 4.0
Lexicographical sorting, and values from duplicate header names combined when iterated.Chrome ? Edge ? Firefox Full support 44IE No support NoOpera Full support 28Safari No support NoWebView Android ? Chrome Android ? Edge Mobile ? Firefox Android No support NoOpera Android ? Safari iOS No support NoSamsung Internet Android ?
Headers() constructor
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ? Firefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Full support 10.1WebView Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile ? Firefox Android No support NoOpera Android Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the preference.
Safari iOS No support NoSamsung Internet Android Full support 4.0
append
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support 14Firefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Full support 10.1WebView Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile Full support YesFirefox Android No support NoOpera Android Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No support NoSamsung Internet Android Full support 4.0
delete
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support 14Firefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Full support 10.1WebView Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile Full support YesFirefox Android No support NoOpera Android Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No support NoSamsung Internet Android Full support 4.0
entries
Experimental
Chrome Full support 45Edge Full support 16Firefox Full support 44IE No support NoOpera Full support 32Safari ? WebView Android Full support 45Chrome Android Full support 45Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 32Safari iOS No support NoSamsung Internet Android Full support 5.0
get
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support 14Firefox Full support 52
Notes
Full support 52
Notes
Notes Prior to Firefox 52, get() only returned the first value in the specified header, with getAll() returning all values. From 52 onwards, get() now returns all values and getAll() has been deleted.
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Full support 10.1WebView Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile Full support YesFirefox Android No support NoOpera Android Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No support NoSamsung Internet Android Full support 4.0
getAll
ExperimentalDeprecatedNon-standard
Chrome No support 42 — 60Edge Full support YesFirefox No support 39 — 52
No support 39 — 52
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera No support 29 — 47Safari No support NoWebView Android No support 42 — 60Chrome Android No support 42 — 60Edge Mobile Full support YesFirefox Android No support NoOpera Android No support 29 — 47Safari iOS No support NoSamsung Internet Android Full support 4.0
has
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support 14Firefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No support NoWebView Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile Full support YesFirefox Android No support NoOpera Android Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No support NoSamsung Internet Android Full support 4.0
keys
Experimental
Chrome Full support 45Edge Full support 16Firefox Full support 44IE No support NoOpera Full support 32Safari ? WebView Android Full support 45Chrome Android Full support 45Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 32Safari iOS No support NoSamsung Internet Android Full support 5.0
set
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support 14Firefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No support NoWebView Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile Full support YesFirefox Android No support NoOpera Android Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No support NoSamsung Internet Android Full support 4.0
values
Experimental
Chrome Full support 45Edge Full support 16Firefox Full support 44IE No support NoOpera Full support 32Safari ? WebView Android Full support 45Chrome Android Full support 45Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 32Safari iOS No support NoSamsung Internet Android Full support 5.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also

 

Мітки документа й учасники

Зробили внесок у цю сторінку: mdnwebdocs-bot, khomyakdi
Востаннє оновлена: mdnwebdocs-bot,