P
P
Programming2020-12-30 16:21:08
AJAX
Programming, 2020-12-30 16:21:08

How to handle this ajax error?

My code is very simple:

var запрос = new XMLHttpRequest()
запрос.open('POST',  /'1.php')  

запрос.onreadystatechange = function()
        {
            if(запрос.readyState === 4)
                { 	  
                    var ответ = JSON.parse(запрос.responseText)
                    alert(запрос.responseText.length)

The response is always in the form of JSON
But if the server to which the client accessed does not exist, then the request.responseText = a string of 320 or 318 characters (on different servers)
, that is, the client accesses a non-existent server and JSON.parse cannot parse the response and throws an error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2020-12-30
teacher @tosrer

Check this out and then pull the
Status
as it is

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question