Sign in Registration

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).


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 (); ('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 (); ('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, отучитель от зависимости и т.д.:
Available on Google Play

Amessage (Communication)

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to



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.