【发布时间】:2017-06-20 16:59:24
【问题描述】:
我有以下 Angular 2 http 请求;
const endpoint = '<auth_url>';
const body = {
prompt: 'consent',
grant_type: 'authorization_code',
redirect_uri: '<redirect_url>',
code: '<authorization_code>'
};
const headers = new Headers();
headers.append('Authorization', 'Basic <auth>');
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const options = new RequestOptions({ headers });
this.http.post(endpoint, body, { headers: headers })
.map(res => res.json())
.subscribe(
data => console.log(JSON.stringify(data)),
error => console.error(JSON.stringify(error)),
() => console.log('Request complete')
);
这是连接到已附加到 ExpressJS 实例的node-oidc-provider,我遇到的问题是 CORS,因为由于预检,请求最终在服务器上显示为 OPTIONS。这不应该是这种情况,因为我已经指定了Content-Type 标题,如上所示。烦人的是,我想弄清楚这是服务器问题还是我的 Angular2 代码的问题?
我是否需要在 ExpressJS 应用程序上显式启用 CORS,如果不需要,为什么在 POST 上设置正确的标头无效?
【问题讨论】:
-
需要在服务器上配置CORS。在 JS 中你无能为力(因此 Angular 也不能)。在某些情况下,它可以使用 JSONP 解决,但这非常有限。
-
谢谢,请看我对下面答案的评论
标签: node.js http angular post cors