【发布时间】:2017-11-06 10:27:05
【问题描述】:
我正在尝试使用 Aurelia 的 Fetch Client 向所有 GET 和 POST 请求添加自定义标头。以下代码(在 app.js 构造函数中)用于设置基本 URL,但标头没有按我想要的方式工作:
constructor(httpClient) {
// set up httpClient
httpClient.configure(config => {
config
.withBaseUrl(localsettings.api)
.withDefaults({
credentials: 'include',
headers: {
'my_appkey': 'f2eabc5e7de-a4cdc857e'
}
})
});
this.httpClient = httpClient;
}
用法:
this.httpClient.fetch(suburl, {
credentials: 'include'
}).then(response => { ... });
通过 Chrome 的开发工具,我可以看到“my_appkey”标头存在,但它没有被创建,因为它是自己的标头,并且它的值不可见:
请求标头:
OPTIONS /index.php/api/v1/keys HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://localhost:9000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Access-Control-Request-Headers: my_appkey
Accept: */*
Referer: http://localhost:9000/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8,es-419;q=0.6,es;q=0.4
我做错了什么?为什么我的自定义标头被移动到Access-Control-Request-Headers?
【问题讨论】:
标签: javascript cors aurelia fetch-api