【发布时间】:2015-12-03 08:09:45
【问题描述】:
我正在根据本教程设置一个两层 angular/sails 应用程序:http://www.pluralsight.com/courses/two-tier-enterprise-app-api-development-angular-sails。对于开发,我的后端/帆服务器是 localhost:1337,我的前端/角服务器是 localhost:8000。前端是一个 gulp-webserver,为所有 API 请求配置了代理以进行航行。我的印象是,这将减轻对特殊角度路线的任何需求,以满足 CORS 问题。但是,当单击应该打开 twitter auth 窗口的链接时,我得到一个空白屏幕,并且在 chrome dev 中我看到:
XMLHttpRequest cannot load https://api.twitter.com/oauth/authorize?oauth_token=oPMDYAAafAAAAhT6lTlwk. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
我已经阅读了关于 CORS 的风帆文档,并且现在允许 CORS 用于 *。 http://sailsjs.org/documentation/concepts/security/cors
Sails CORS 配置:
module.exports.cors = {
allRoutes: true,
origin: '*',
credentials: true,
methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',
// headers: 'content-type'
};
gulp-webserver 代理配置:
gulp.task('serve', ['copyAll'], function() {
return gulp.src(paths.temp)
.pipe(webserver({
livereload: true,
proxies: [{
source:'/api',
target:'http://localhost:1337'
}]
}));
});
Angularjs 控制器:
angular.module('app', ['satellizer'])
.config(function ($authProvider) {
$authProvider.twitter({
url: '/api/user/login'
});
});
响应标头:
access-control-allow-credentials:true
access-control-allow-origin:http://127.0.0.1:8000
connection:keep-alive
content-length:113
content-type:text/plain; charset=UTF-8
date:Mon, 07 Sep 2015 18:36:14 GMT
location:https://api.twitter.com/oauth/authorize?oauth_token=7A8gtyfhgghfyuAhaeEAAABT6kWRtw
vary:X-HTTP-Method-Override, Accept
x-powered-by:Sails <sailsjs.org>
有什么建议吗?
【问题讨论】:
-
这可能是我与 twitter API 通信方式的问题吗?
-
是的,您的sails 方法似乎将您的客户重定向到twitter,它不允许您做前端(因此是cors 消息)。你如何在风帆中调用 twitter?
-
来自我的用户控制器...这是要点:gist.github.com/CiscoKidxx/bd7b4a01edfc55ec9e09
-
@Jorg - 我正在使用请求库并从后端的 UserController 进行 POST。这是gist.github.com/CiscoKidxx/bd7b4a01edfc55ec9e09
-
我发现了这个问题,但它是如此无关紧要,我想我可能会删除这个问题。
标签: angularjs cors sails.js gulp