自定义响应的标头从transformResponse标头参数($ resource)中丢失 [英] Custom response's headers are missing from transformResponse headers argument ($resource)

查看:84
本文介绍了自定义响应的标头从transformResponse标头参数($ resource)中丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为API端点定义了一个 $ resource ,该API端点返回一个包含多个 headers 的响应,但该响应位于

I've defined an $resource to an API endpoint that returns a response containing several headers but in the transformResponse config function most of headers are missing from the headersGetter function argument.

我该如何解决?

API的响应标头

HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
x-frame-options: DENY
Access-Control-Allow-Origin: http://localhost:9000
access-control-allow-methods: POST, PUT, GET, DELETE, OPTIONS
access-control-allow-headers: Content-Type
Access-Control-Allow-Credentials: true
Content-Disposition: attachment; filename="testCall.pcap"
FileName: testCall.pcap
Content-Type: application/pcap

transformResponse 的标题

transformResponse's headers

Pragma: no-cache
Content-Type: application/pcap
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0


app.factory("MyService", function ($resource, ENV, _) {
    return {
        testCall: $resource(ENV.apiEndpoint + "/test-call", {}, {
            launch: {
                method: 'POST',
                isArray: false,
                headers:{'Accept':'application/octet-stream'},
                responseType: 'blob',
                cache: false,
                transformResponse: function(data, headers){
                    var filename = headers('Content-Disposition'); //headers('FileName')
                    var contentType = headers('Content-Type');

                    var file = new Blob([data], {
                        type: contentType
                    });

                    var fileURL = URL.createObjectURL(file);
                    var a         = document.createElement('a');
                    a.href        = fileURL;
                    a.target      = '_blank';
                    a.download    = filename;
                    document.body.appendChild(a);
                    a.click();
                }
            }
        }),
        searchOptions: $resource(ENV.apiEndpoint + "//search-options")
    };
});

推荐答案

假设您正在进行CORS调用,则响应标头并未全部公开.服务器端需要在CORS过滤器中添加响应标头"Access-Control-Expose-Headers".

Assume you are making CORS calls, response headers are not all exposed. Server-side needs to add response header "Access-Control-Expose-Headers" in CORS filter.

例如要通过CORS调用读取名为"X-MY-HEADER1"和"X-MY-HEADER2"的自定义响应标头,请添加服务器标头

e.g. To read a custom response header named "X-MY-HEADER1" and "X-MY-HEADER2" with CORS call, server add header

访问控制公开标题:"X-MY-HEADER1,X-MY-HEADER2"

通过@nancoder查看答案,网址为 https://stackoverflow.com/a/23726352/4684232

See the answer by @nancoder at https://stackoverflow.com/a/23726352/4684232

这篇关于自定义响应的标头从transformResponse标头参数($ resource)中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆