0

i am developing OEMBED app where i want to request some OEMBED api to get me JSON data. I am using node/ express in backend and angular in front end.

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.

I know i have to do something with CORS but don't know how.

i am using chrome and localhost development environment.

  • CORS helps the remote site to specify whether it wants to allow you to access its data via JavaScript – so in this case Instagram would have to implement this for you to get access to their data via AJAX. – CBroe Sep 19 '14 at 10:23
  • Hi they have implemented this. it is mentioned in http://oembed.com and if i hit this url in browser http://api.instagram.com/oembed?url=http://instagram.com/p/V8UMy0LjpX/&format=json then i get the data return by server. – Codeforgeek Sep 19 '14 at 10:25
  • Of course you get the data when you hit the URL directly in your browser – because it’s not a JavaScript-made request then. // It rather seems that you should specify a callback function to request the data as JSONP instead. – CBroe Sep 19 '14 at 10:36
  • i tried this in my angular code. $http.jsonp("http://api.instagram.com/oembed?url=http://instagram.com/p/V8UMy0LjpX/&format=json"). success(function(data, status, headers, config) { alert("works"); }). error(function(data, status, headers, config) { alert("not works"); }); It shows not works ! :( – Codeforgeek Sep 19 '14 at 10:49
  • I had the same issue. I suggest this workaround https://stackoverflow.com/a/25354788/2575875 – Mert Aksoy May 29 '20 at 17:12

1 Answers1

0

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

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 i.e. the instagram one (and not your local server)

V31
  • 7,626
  • 3
  • 26
  • 44
  • i use $http.jsonp("http://api.instagram.com/oembed?url=http://instagram.com/p/V8UMy0LjpX/&format=json"). success(function(data, status, headers, config) { alert("works"); }). error(function(data, status, headers, config) { alert("not works"); }); and i am getting response from them as JSON but don;t know how to access it because alert showing not works message. – Codeforgeek Sep 19 '14 at 11:08
  • that means you not getting the response that you desire and it is failing over it – V31 Sep 19 '14 at 11:10