Answer the question
In order to leave comments, you need to log in
Where is the error or what am I doing wrong?
There is a JSON request in the form of a url
var newArr = JSON.parse(req.responseText);
}
catch (e) {
document.getElementById("PRVDATA").innerHTML = "Ошибка разбора ответа от сервера: " + e +"<br>" +req.responseText;
}
function escapeHtmlChars(req) {
return req
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
for (var i = 0; i < newArr.length; i++) {
newArr[i].correct = escapeHtmlChars(newArr[i].correct);
for (var j = 0; j < newArr[i].choices.length; j++) {
newArr[i].choices[j] = escapeHtmlChars(newArr[i].choices[j]);
}
}
Answer the question
In order to leave comments, you need to log in
Perhaps you did not receive json in the response, but some other text. For example, if you are not authorized, then some services may redirect to the authorization page, and then the json parser dies. Try using fetch
instead of JSON.parse
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question