Agu*_*tín 3 http same-origin-policy node.js angularjs sails.js
我正在尝试使用前端的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.
Run Code Online (Sandbox Code Playgroud)
我的请求看起来像是:
$http.post('http://remoteserver:1337/login/', { email: userData.email, password: userData.password});
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您需要服务器启用CORS(跨源资源共享),
来自Sails.js cors文档(http://sailsjs.org/documentation/concepts/security/cors)
要允许来自任何域的跨源请求到您应用中的任何路由,只需在config/cors.js中启用allRoutes:
allRoutes: true
以下是关于CORS是什么以及为什么需要它的简要说明:
http://enable-cors.org/index.html