P
P
pthon2021-05-01 13:19:57
AJAX
pthon, 2021-05-01 13:19:57

Why does xhr.status return undefined?

Hello, why is xhr.status not defined when there are errors on the server? The server returns the correct response code.
Send code:

$.ajax({
            url:     path,
            type:     "POST", //метод отправки
            dataType: "html", //формат данных
            data: $(formID).serialize(),  // Сеарилизуем объект
            success: function(response, textStatus, xhr) { //Данные отправлены успешно
                switch (xhr.status) {
                    case 200:
                        alert("Запрос успешно выполнен.");
                        break;
                    default:
                        alert("Сервер вернул неизвестный код ответа");
                        break;
                }
            },
            error: function(response, textStatus, xhr) { // Данные не отправлены
                switch (xhr.status) {
                    case 401:
                        alert('ОШИБКА: Вы не авторизованны, либо у вас нет прав администратора.');
                        break;
                    case 400:
                        alert('ОШИБКА: Какие-то поля не заполнены.');
                        break;
                    case 520:
                        alert('ОШИБКА: Возможно, проблемы с БД.');
                        break;
                    case 409:
                        alert('ОШИБКА: Возможно, такой пользователь существует.');
                        break;
                    default:
                        alert('Сервер вернул неизвестную ошибку. Код: ' + xhr.status);
                        break;
                }
            }
        });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pthon, 2021-05-01
@pthon

response.status

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question