如何在AJAX中获取响应头

时间:2022-10-09 00:22:54

I am using the following code for an AJAX call

我正在使用以下代码进行AJAX调用

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

这是仅打印内容类型。在开发人员控制台中,我可以看到响应中的标头数量。我怎样才能在AJAX中获得这些头文件

如何在AJAX中获取响应头

3 个解决方案

#1


14  

It's seems no problem. I tried and got it works.

这似乎没问题。我试过并让它运作起来。

Use JSONPlaceholder and here is my code

使用JSONPlaceholder,这是我的代码

$.ajax({
  url: root + '/posts/1',
  headers: {'test': 'test'},
  method: 'GET'
}).then(function(data, status, xhr) {
  console.log(xhr.getAllResponseHeaders());
});

the result is

结果是

Pragma: no-cache
Date: Wed, 23 Dec 2015 06:36:57 GMT
Via: 1.1 vegur
X-Content-Type-Options: nosniff
Server: Cowboy
X-Powered-By: Express
Vary: Origin
Content-Type: application/json; charset=utf-8
Cache-Control: no-cache
Access-Control-Allow-Credentials: true
Content-Length: 292
Etag: W/"124-yv65LoT2uMHrpn06wNpAcQ"
Expires: -1

#2


5  

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.

然后您就可以在浏览器中访问它。

#3


3  

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!!!!

问题不在于Jquery或Ajax。响应标头由服务器设置。您发送请求的服务器未设置标头!简单!!!!

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

当我尝试使用我的服务器[某些响应模糊以确保安全]时,我得到了以下输出

如何在AJAX中获取响应头

#1


14  

It's seems no problem. I tried and got it works.

这似乎没问题。我试过并让它运作起来。

Use JSONPlaceholder and here is my code

使用JSONPlaceholder,这是我的代码

$.ajax({
  url: root + '/posts/1',
  headers: {'test': 'test'},
  method: 'GET'
}).then(function(data, status, xhr) {
  console.log(xhr.getAllResponseHeaders());
});

the result is

结果是

Pragma: no-cache
Date: Wed, 23 Dec 2015 06:36:57 GMT
Via: 1.1 vegur
X-Content-Type-Options: nosniff
Server: Cowboy
X-Powered-By: Express
Vary: Origin
Content-Type: application/json; charset=utf-8
Cache-Control: no-cache
Access-Control-Allow-Credentials: true
Content-Length: 292
Etag: W/"124-yv65LoT2uMHrpn06wNpAcQ"
Expires: -1

#2


5  

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.

然后您就可以在浏览器中访问它。

#3


3  

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!!!!

问题不在于Jquery或Ajax。响应标头由服务器设置。您发送请求的服务器未设置标头!简单!!!!

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

当我尝试使用我的服务器[某些响应模糊以确保安全]时,我得到了以下输出

如何在AJAX中获取响应头