我正在尝试在前端使用 AngularJS 向运行 Sails JS (NodeJS) 的远程服务器发出请求,但我收到了这个原始“空”错误:
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 以及为什么需要它的简要说明:
关于angularjs - 对远程 Sails 服务器的 Angular JS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32082451/