-3

I have built a REST API with Node.js Express http://localhost:3000/api/feeds with node.js and filled with data.

router.get('/api/feeds', function (req, res, next) {
    documentDBConfig.getAllDocuments()
        .then(() => res.json(documentDBConfig.feedsArray));
});

enter image description here

Now i make a static website and want to use javascript or jquery to get data from my REST API. I used this code

$.getJSON( "http://localhost:3000/api/feeds", function( data ) {
    console.log(data);
});

But it keeps saying

XMLHttpRequest cannot load http://localhost:3000/api/feeds. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.

I know i'm doing it wrong, but i couldn't find the correct way. How can i get this json content with my website from my REST API (http://localhost:3000/api/feeds) ?

Edit: I don't get this warning with explorer, but i can not get the content. And now i solved the chrome problem thus i don't get this warning anymore. But i can't read the content. That is not a duplication.

Now i get this warning

Failed to load resource: the server responded with a status of 404 (Not Found)
Pythonist
  • 115
  • 2
  • 12
  • 1
    Possible duplicate of ["No 'Access-Control-Allow-Origin' header is present on the requested resource"](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) – Deividas Mar 13 '17 at 08:28
  • @Deividas Karžinauskas yes have read this answers, but am i doing the rest correct? I mean can i read a url content with this jquery function? – Pythonist Mar 13 '17 at 08:31
  • Your jquery function is good. You need to update your server headers to allow requests form other urls. Read the resources suggested by the post. – Deividas Mar 13 '17 at 08:33
  • @Deividas Karžinauskas but i'm not getting this warning with explorer and i can also not getting the json content too. – Pythonist Mar 13 '17 at 08:37

3 Answers3

0

This is because you are accessing a resource from another domain. You try to access http://localhost:3000 from http://localhost:63342. You can read more about this here https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Access-Control-Allow-Origin

Robert Sandu
  • 673
  • 1
  • 6
  • 15
0

Can you show us your REST api code so we can help you ? You need to set some headers in your backend to allow requests coming from other origins.

If you happen to be using express, this will help you. But you could have built the REST api in another way, so please provide us with more information.

realappie
  • 4,656
  • 2
  • 29
  • 38
  • i have added the code app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); }); but nothing has changed – Pythonist Mar 13 '17 at 09:10
  • Thanx Abdel, i got the data in explorer wel in this way. With chrome it doesnt work – Pythonist Mar 13 '17 at 09:48
  • @LeventOner No problem! You were executing the request correctly all along. Next time make sure you do enough googling before you start a question. People dislike questions that have been often answered. – realappie Mar 13 '17 at 10:49
0

Basically you are performing a CORS request which means you are trying to call a resource on different server. So your REST api should allow CORS requests by adding the response headers allowing the UI server resource. Please Refer to this question if it helps

Community
  • 1
  • 1
Sampath
  • 599
  • 5
  • 12