Answer the question
In order to leave comments, you need to log in
How to get JSON data?
Hello.
There is such a country.io/names.json
How to get this data in pure JS, what would be further use of it?
Answer the question
In order to leave comments, you need to log in
On pure JS - browsers have had fetch api for a long time:
fetch('http://country.io/names.json')
.then(r => r.json())
.then(names => console.log('Names arrived!', names)
On pure JS, like this
, the address itself, where to knock, you have
var names = {} //тут у нас будет результат
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://country.io/names.json', false);
xhr.send();
if (xhr.status != 200) {
// обработать ошибку
alert( xhr.status + ': ' + xhr.statusText ); // пример вывода: 404: Not Found
} else {
// вывести результат
names = xhr.responseText;
}
You most likely have a CORS error and the server is not returning the Access-Control-Allow-Origin: * header. If you don't need to send any headers to the server try using jsonp. https://www.npmjs.com/package/fetch-jsonp - maybe suitable for react
https://learn.javascript.ru/ajax-jsonp - js
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question