0

I am trying to receive an XML and JSON file from a webpage, but I am receiving the error:

Access to XMLHttpRequest at 'http://cis.csuohio.edu/~sschung/CIS408/OnebusinessDataFormat_yelp.json' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have tried to set a response header for xobj.setRequestHeader('Access-Control-Allow-Origin','*');, but that still doesn't fix the problem.

Here is my code for the requests:

XML Request:

function loadXML(callback) {   

        var xobj = new XMLHttpRequest();
            xobj.overrideMimeType("application/xml");
        xobj.open('GET', 'http://cis.csuohio.edu/~sschung/CIS408/restaurant_menu.xml', true); 
        xobj.onreadystatechange = function () {
              if (xobj.readyState == 4 && xobj.status == "200") {
                // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
                callback(xobj.responseText);
              }
        };
        xobj.setRequestHeader('Access-Control-Allow-Origin','*');
        xobj.send(null);  
}

JSON Request:

function loadJSON(callback) {   

        var xobj = new XMLHttpRequest();
            xobj.overrideMimeType("application/json");
        xobj.open('GET', 'http://cis.csuohio.edu/~sschung/CIS408/OnebusinessDataFormat_yelp.json', true); 
        xobj.onreadystatechange = function () {
              if (xobj.readyState == 4 && xobj.status == "200") {
                // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
                callback(xobj.responseText);
              }
        };
        xobj.setRequestHeader('Access-Control-Allow-Origin','*');
        xobj.send(null);  
}

I hope to be able to import the XML and JSON files. Is this an issue with the server? I am new to web programming, but my understanding is that the server is going to have to allow Cross-Origin Requests, yes?

hippoman
  • 187
  • 2
  • 10
  • _Does the server have to allow Cross-Origin Requests?_ Yes – hindmost Mar 26 '19 at 15:20
  • Yes it does, else you can 1°) add a proxy which adds the Cross-Origin requests header, or 2°) use a browser extension to disable Cross-Origin safety warnings but that's just workarounds in case you have no access to server. – Antoine Gautrain Mar 26 '19 at 15:24
  • "I have tried to set a response header for xobj.setRequestHeader('Access-Control-Allow-Origin','*')" — Note how you said you were trying to set a **response** header but the function name says **request** and not **response**. That won't work. – Quentin Mar 26 '19 at 15:34

0 Answers0