如何在AJAX中获取响应头

Ano*_* LL 9 ajax jquery

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

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

        },
        error: function (jqXHR, status) {

        }
    });
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

gao*_*hen 14

这似乎没问题.我试过并让它有效.

使用JSONPlaceholder,这是我的代码

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

结果是

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
Run Code Online (Sandbox Code Playgroud)


use*_*182 7

在您的服务器上,您需要添加

res.header("Access-Control-Expose-Headers", "header-you-want-to-expose");
Run Code Online (Sandbox Code Playgroud)

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


Mr.*_*jun 5

我使用了与你相同的代码

success: function (dt, status, request) {
            console.log(request.getAllResponseHeaders());
Run Code Online (Sandbox Code Playgroud)

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

当我尝试使用我的服务器时[一些响应因安全原因而模糊]我得到以下输出

控制台输出快照