Angular 对远程 Sails 服务器的 JS 请求

Angular JS request to remote Sails server

我正在尝试在前端使用 AngularJS 向远程服务器 运行 Sails JS (NodeJS) 发出请求,但我收到此来源 'null' 错误:

XMLHttpRequest cannot load http://remoteserver:1337/login/. The 'Access-Control-Allow-Origin' header contains the invalid value ''. Origin 'null' is therefore not allowed access.

我的请求看起来像这样:

$http.post('http://remoteserver:1337/login/', { email: userData.email, password: userData.password}); 

有什么想法吗?

您需要服务器启用 CORS(跨源资源共享),

来自 Sails.js cors 文档 (http://sailsjs.org/documentation/concepts/security/cors)

To allow cross-origin requests from any domain to any route in your app, simply enable allRoutes in config/cors.js: allRoutes: true

下面是关于什么是 CORS 以及为什么需要它的简要说明:

http://enable-cors.org/index.html