How to read/get response header from ajax/http/service/api call or response

Henry
2 min readJun 23, 2018

What is response header?

When service return the response it will include several network related parameters. Like request header, response header and actual response of the service or API call. For example below image.

Green color rectangle box is showing the response header values. There are some scenarios in your application where you want to read any of this parameter or all parameter.

Let’s say we want to read the on of the response header value.

There are two ways to do that.

Let’s see the first approach.( For jQuery version 1.3.x or lesser)

  1. Look at this code snippet.
var xhr = $.ajax({
type: "POST",
data: {"key":"value"},
url: "labels.json",
success: function(output, status) {
console.log(xhr.getResponseHeader("Content-Type"));
},
error: function(output) {
console.log("Error in API call");
}
});

In above approach ajax call will return the xhr object. Which consist of getResponseHeader. Here we are passing an argument “Content-Type” and in return we are expecting the value of response header key “Content-Type”. In…

--

--