BrownChiLD BrownChiLD - 1 year ago 68
JSON Question

Javascript weird Fetch() issues, fails on a specific JSON data/format

I have the following code using fetch

var url1 = '';
var url2 = '';

.then(function (result){
console.log("Result received");

return result.json();
// console.log("data received",data[1]['name']);
console.log("data received",data);

console.log("Error in Fetch",error.message);

I'm fetching valid JSON data from both URLs, but with slightly different structure

var url1 = '';
var url2 = '';

If I load data from URL1, things are fine. But if I load url2 i get an error "Failed to fetch"

What gives???

See demo:,console

Answer Source

The issue here is the same-origin policy. If you run this JavaScript on it will work. Or, you can get to respond with an Access-Control-Allow-Origin header to allow any origin or specific origins (such as and

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download