【发布时间】:2015-08-15 09:52:53
【问题描述】:
我在堆栈溢出中看到了很多答案,说设置响应标头会使您发出“CORS”请求。但没有解决方案对我有用。我编写了以下代码:
//Server.js Code
var express = require('express'),
app = express();
app.all('*',function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
next();
我正在尝试在客户端使用 $http 从 URL 访问内容:
//Controller.js
$http.get('http://domainA.com/a/ipadapi.php?id=135&client=ipad').success(function(response){
alert("I got response");
});
它在控制台中显示以下错误。
XMLHttpRequest cannot load http://domainA.com/a/ipadapi.php?id=135&client=ipad The 'Access-Control-Allow-Origin' header has a value 'http://example.xxxxx.com' that is not equal to the supplied origin. Origin 'http://localhost:3000' is therefore not allowed access.
注意:我是 nodeJS、Express 和 AngularJs 的新手
【问题讨论】:
-
在你的服务器上设置cors不会在其他服务器上启用cors,目标服务器必须启用它
-
您是否尝试从 localhost:3000 访问 domainA.com ?如果是这样,您是 domainA.com 的所有者吗?从我所看到的,您必须在 domainA.com 服务器的请求中添加标头。几个小时前我已经回答了同样的问题:stackoverflow.com/questions/30577886/…
-
我不是 domainA.com 的所有者。那么,我如何从该 URL 访问内容我是否需要通过 example.xxxxx.com 而不是本地主机运行我的应用程序? @cl3m
标签: javascript angularjs node.js express cors