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?