1

I did a simple node server , which I need it to be Cross Origin for $http.get() calling , so I add the required headers to the app.use as details in this answer. Finally the server code is -

var express = require('express');
var app = express();

var port = process.env.PORT || 3000;

app.use(express.static('.'));

app.all('/', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
 });

app.listen(port);
console.log('server runs on port: ' + port); 

In my index.html there is angular $http.get() calling to a another origin -

$http.get('http://itunes.apple.com/search', {
    term: 'johnson'
});

but still this calling is denied by the browser and return error -

 XMLHttpRequest cannot load https://itunes.apple.com/search. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

How could I make Cross Origin $http.get calling from the index.html correctly ?

Community
  • 1
  • 1
URL87
  • 10,667
  • 35
  • 107
  • 174
  • 1
    Why do you allow CORS on your own webserver and wait it would be allowed on Apple server? – Vladimir G. Jul 15 '16 at 06:42
  • I'm new in node , please give me an solution if yoe have .. – URL87 Jul 15 '16 at 06:46
  • 1
    Short answer: it is imposible to make cross-domain requests from your webpage while CORS is not allowed on the server. Since you haven't control on itunes you cannot do it – Vladimir G. Jul 15 '16 at 06:48

1 Answers1

2

You actually don't understand how cross-origin resource sharing works. Cross-origin requests should be allowed on server you making requests to, not on your server from which you are serving your page.

So, the answer is you can't make request to apple website if they haven't allowed you to do it.

P.S. You have allowed other websites to make request to your website by adding this code to your express app:

  app.all('/', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
Sergey
  • 1,244
  • 1
  • 9
  • 4