Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Response headers in AJAX

Tags:

jquery

ajax

I am using the following code for an AJAX call

 $.ajax({
        type: "POST",
        url: "http://******/cxf/view/*****",
        data: {*****},
        headers: {*****},
        success: function (dt, status, request) {
            console.log(request.getAllResponseHeaders());

        },
        error: function (jqXHR, status) {

        }
    });

This is printing only content-type. In the developer console i can see number of headers in the response. How can i get those headers in AJAX

enter image description here

like image 874
Anoop LL Avatar asked Dec 23 '15 06:12

Anoop LL


2 Answers

On your server you need to add

res.header("Access-Control-Expose-Headers", "header-you-want-to-expose");

and then you will be able to access it in the browser.

like image 38
user1071182 Avatar answered Sep 16 '22 14:09

user1071182


I used the same code you used

success: function (dt, status, request) {
            console.log(request.getAllResponseHeaders());

Problem is not with Jquery or Ajax. Response headers are set by Server. The server you are sending the request to, is not setting the headers! Simple!!!!

When i tried with my server [Some responses blurred for security] i got the following output

Snap of Console output

like image 196
Arjun Avatar answered Sep 18 '22 14:09

Arjun