Sign in Registration
ArticlesDownloadServicesAudioContacts
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

Communication

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

Share this

Subscribe to