2

Не могу распарсить json - выдает ошибку

Uncaught SyntaxError: Unexpected end of JSON input.

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json', true);

xhr.onreadystatechange = function(){
    if (xhr.readyState != 4){
        return;
    }

    if (xhr.status == 200){

    }
    else {
        alert('shit happens: ' +  xhr.status + ', ' + xhr.statusText );
    }
}
xhr.send();
xhr = JSON.parse(xhr.responseText);

1 ответ 1

3
if (xhr.status == 200){
  var result = JSON.parse(xhr.responseText); // и не раньше

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.