我有以下Angular 2 http请求;Angular 2 CORS问题
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解决这个问题,但这非常有限。 –
谢谢,请参阅我的评论下面的答案 – MindVox