0

I'm building a small webpage that gets data from several different rest API's (on different domain) and compares the data.

When I open the html file locally everything works fine, but when I upload to the server and try to open it, the data from 1 of the API's doesnt display and the firefox console gives me this error message:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at . (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

(other browsers display a different message but none of them display the api data)

this is the code for the request:

function ajax(url){
return new Promise(function(resolve, reject) {
  var req = new XMLHttpRequest();
  req.open('GET', url);
  req.onload = function() {
    if (req.status == 200) {
      // Resolve the promise with the response text
      resolve(req.response);
    }
    else {
      // Otherwise reject with the status text
      // which will hopefully be a meaningful error
      reject(Error(req.statusText));
    }
  };
  // Handle network errors
  req.onerror = function() {
    reject(Error("Network Error"));
  };
  // Make the request
  req.send();
});

}

I have been reading up on xmlhttprequest and setrequestheader, but i cant seem to figure it out on my own.

-why is it that the httprequest to the rest api works locally but not from server? -what do i need to change to make it work?

Diego
  • 19
  • 3
  • 1
    Possible duplicate of [Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present) – tao Jan 15 '18 at 22:47
  • how are you making the request from the server ? what type of web server is it ? – fuzzybear Jan 15 '18 at 22:50
  • @saj i have added the code of the request to the OP. the site is hosted on 000webhost.com – Diego Jan 15 '18 at 23:05

0 Answers0