gpt4 book ai didi

angularjs - transformResponse header 参数 ($resource) 中缺少自定义响应的 header

转载 作者:行者123 更新时间:2023-12-04 10:36:25 27 4
gpt4 key购买 nike

我定义了一个 $resource到 API 端点,该端点返回包含多个 headers 的响应但在 transformResponse headersGetter 中缺少大部分头文件的配置功能函数参数。

我该如何解决?

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的标题
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 调用,响应 header 并未全部公开。服务器端需要在 CORS 过滤器中添加响应头“Access-Control-Expose-Headers”。

例如要使用 CORS 调用读取名为“X-MY-HEADER1”和“X-MY-HEADER2”的自定义响应 header ,服务器添加 header
Access-Control-Expose-Headers: "X-MY-HEADER1, X-MY-HEADER2"
请参阅@nancoder 的回答 https://stackoverflow.com/a/23726352/4684232

关于angularjs - transformResponse header 参数 ($resource) 中缺少自定义响应的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33257320/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com