我有一个在端口3000上运行Passport配置的环回API服务器。我的UI由反应JS组成,它从端口3080提供。我想用Google实现身份验证。为此,从我的反应应用程序,我打电话给
localhost:3000/auth/google
但由于CORS错误,调用未被重定向到谷歌:
Fetch API cannot load http://localhost:3000/auth/google. Redirect from 'http://localhost:3000/auth/google' to 'https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=h…d=584602753475-1kk345gd8trcn31pbg1v2gncajhpakk8.apps.googleusercontent.com' has been blocked by CORS policy: Request requires preflight, which is disallowed to follow cross-origin redirect.
当我从新的浏览器标签中点击http://localhost:3000/auth/google
网址时,一切正常。
任何人都可以告诉我应该在哪里启用cors?
修改
我可以看到,浏览器正在向localhost发出选项请求:3000 / auth / google
这是我用curl发出选项请求时得到的结果:
HTTP/1.1 204 No Content
X-Powered-By: Express
Vary: Origin
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE
Access-Control-Max-Age: 86400
Date: Mon, 20 Feb 2017 14:05:53 GMT
Connection: keep-alive
答案 0 :(得分:0)
我不知道如何在Strongloop或ReactJS中启用CORS,但要启用它,您需要两件事:
1. The right configuration of the JS request like so:
var params = {
…
mode: 'cors',
credentials: 'include' //<- that gave me some headache a while ago,
//but is probably not necessary in your case.
…
}
fetch(new Request(<url>, params).then(res => …);
我希望有所帮助。