如何在跨域API的OPTIONS
请求中添加标题?
我正在使用的API要求在所有请求中设置JWT令牌作为Authorization
标头。
当我尝试访问API时,Angular首先执行一个OPTIONS
请求,这个请求不关心我为"真实"请求设置的标题,就像这样:
this._headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer my-token-here'
});
return this._http
.post(AppConfig.apiUrl + 'auth/logout', params, {headers: this._headers})
...
...
当未提供令牌时,API返回HTTP状态401,Angular认为OPTIONS
请求失败。