0
我知道有很多关于如何在node/express中启用CORS的东西,并试图实现这些,但没有运气。cors angular/node + express issue
我的角度要求:
function getPhotos(location) {
var url = 'https://api.instagram.com/v1/media/search?lat=' + location.lat + '&lng=' + location.lng + '&access_token=' + ig_access_token;
$http({
method: 'GET',
url: url
}).then(function (response) {
console.log("here are the photos: ", response);
}, function(err) {
console.log("whoops, there is an error: ", err);
});
和节点/快递我的服务器设置:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.use(express.static(__dirname + '/public'));
app.listen(port);`
但我仍然不断收到此错误:
否“接入控制 - Allow-Origin'标题出现在请求的资源上。因此'Origin'http://localhost:8080'不允许访问。
任何想法?
CORS这样做需要正在接收请求的服务器上启用。在Express服务器上启用它并没有什么区别。 – skirtle
使用适用于Express的CORS软件包:https://github.com/expressjs/cors –