【发布时间】:2018-01-05 19:16:35
【问题描述】:
我正在努力通过 CORS 获取授权:
const token = 'this.is.secret!';
fetch('http://corsserver/api/hello', {
method: 'get',
credentials: 'include',
mode: 'cors',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(response => {
console.log(response);
}).catch(error => {
console.log(error);
});
当我运行此请求时,Chrome 将标头设置为:
Request Method:OPTIONS
我查了一下,it's a preflighted request。
我的意思是,这真的很酷。但是我不知道在预检回来后如何发送实际请求好吧!下一步是什么?如何发送 GET 请求?
我在这里遗漏了一些非常基本的东西。
【问题讨论】:
-
附带点:似乎没有必要为
GET请求发送Content-Type请求标头……GET中没有正文来提供内容类型…… -
我猜 OPTIONS 可能会失败,因为服务器需要对该 OPTIONS 请求进行身份验证。查看我刚刚添加到答案的更新
标签: javascript cors fetch-api preflight