Sign in Registration
ruen

How to send or receive HTTP headers in JavaScript

Typically a server-side language is used to work with headers, but you can also try JavaScript (JS).

js-get-send-http-headers

How to send HTTP request headers

You can send JavaScript headers using Ajax requests. In a pure language, without using libraries, it might look like this:

  var request = new XMLHttpRequest ();
request.open ('GET', document.location, false);
request.setRequestHeader ('Accept', 'text / plain');
request.setRequestHeader ('Content-Type', 'text / plain');
request.setRequestHeader ('Content-Language', 'en-US');
request.send (null);
request.getAllResponseHeaders (). toLowerCase ();  

How to get HTTP response headers

Getting headers in JavaScript is more difficult, since this language works on the client side. But you can send a request to the server using Ajax and get roughly similar headers using the getAllResponseHeaders function. Sample code:

  var request = new XMLHttpRequest ();
request.open ('GET', document.location, false);
request.send (null);
request.getAllResponseHeaders (). toLowerCase ();  

Thus, you can send or receive headers using JavaScript, and not only using server-side languages, like PHP .

Comments (0)
For commenting sign in or register.
Оставить заявку

Latest articles

Popular sections

Eqsash (Tools)

Android app - VK LAST USER ID, отучитель от зависимости и т.д.:
Скачать APK

Share this

Subscribe to

YouTube

Books

IT notes - In simple language about the most necessary things (HTML, CSS, JavaScript, PHP, databases, Drupal, Bitrix, SEO, domains, security and more), PDF, 500 p.

Am (Мессенджер)

Общение смайликами со звуком.

Не работает видео? Смотреть в VK

- Download APK-файл (Android);

- Веб-версия (и PWA);

- Page проекта.