[英]XMLHttpRequest cannot load <instagram OEMBED URL>. No 'Access-Control-Allow-Origin' header is present on the requested resource
i am developing OEMBED app where i want to request some OEMBED api to get me JSON data. 我正在开发OEMBED应用,我想请求一些OEMBED API来获取JSON数据。 I am using node/ express in backend and angular in front end.
我在后端使用node / express,在前端使用angular。
Here is my server code. 这是我的服务器代码。
var express = require("express"); var app =express(); var allowCrossDomain = function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // intercept OPTIONS method if ('OPTIONS' == req.method) { res.send(200); } else { next(); } }; app.use(allowCrossDomain); app.use(express.static(__dirname + '/js')); app.get('/',function(req,res,next){ res.sendfile('index.html'); }); app.listen(3000,function(){ console.log("We are listening at 3000"); });
and my angular code
和我的角度代码
app.controller('status_controller',function($scope,$http){ $scope.add_status=function(){ $http.get("http://api.instagram.com/oembed?url=http://instagram.com/p/V8UMy0LjpX/&format=json").success(function(data){ console.log(data); }); }; });
After executing i am getting this error in console.
执行后,我在控制台中收到此错误。
XMLHttpRequest cannot load http://api.instagram.com/oembed? url=http://instagram.com/p/V8UMy0LjpX/&format=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://<<localhost>>:3000' is therefore not allowed access.
I have tried node package such as cors and express-cors and i also tried suggestion from similiar post in SO but nothing helps me.
我尝试了诸如cors和express-cors之类的节点程序包,也尝试了SO类似文章中的建议,但没有任何帮助。
I know i have to do something with CORS but don't know how. 我知道我必须对CORS做些事情,但不知道如何做。
i am using chrome and localhost development environment. 我正在使用chrome和localhost开发环境。
If you using AngularJS<1.2 you need to delete the 'X-Requested-With' header and set useXDomain to true in order to make it work 如果您使用的是AngularJS <1.2,则需要删除“ X-Requested-With”标头,并将useXDomain设置为true才能使其正常工作
yourModule
.config(function($httpProvider){
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
});
otherwise this is not required in higher versions. 否则,在更高版本中不需要。 The server needs to setup with the crossDomain stuff ie the instagram one (and not your local server)
服务器需要设置crossDomain的东西,即instagram的东西(而不是您的本地服务器)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.