Używam API pobierania HTML5.Zezwalaj na nagłówek Access-Control-Allow-Origin za pomocą interfejsu HTML5 Fetch API
var request = new Request('https://davidwalsh.name/demo/arsenal.json');
fetch(request).then(function(response) {
// Convert to JSON
return response.json();
}).then(function(j) {
// Yay, `j` is a JavaScript object
console.log(JSON.stringify(j));
}).catch(function(error) {
console.log('Request failed', error)
});
Jestem w stanie używać normalnego json, ale nie mogę pobrać danych z powyższego adresu URL. To wyrzuca błąd:
Fetch API cannot load https://davidwalsh.name/demo/arsenal.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Serwer zewnętrzny musi go ustawić, nie można nic zrobić po stronie klienta. – epascarello
@epascarello: Możemy zrobić po stronie klienta. Za sceną trwa żądanie XHR. Sprawdź to 'https: // davidwalsh.name/fetch' – iNikkz