P
P
poimanoo2018-08-30 21:43:43
JavaScript
poimanoo, 2018-08-30 21:43:43

How to display the error returned by the server (ajax post request)?

Hello. You need to register a user by sending his data to the server. The request reaches the server (there is a counter of incoming requests), but success does not work, and the error text cannot be displayed. I give the request code below, the screen displays "undefined", 0, error and an empty window, respectively:

$('.auth-button').on('click', function() {
  $.ajax
  ({ 
      url:url1,
      type:"POST",		 
      data:JSON.stringify({"email":$('#email').val(),"password": $('#pass').val()}),
      contentType:"application/json",
      dataType:"json",
      success: function(){
          window.location = url2;
      },
      error: function(xhr, status, error) {
          alert(xhr.ResponseText);
    alert(xhr.status);
          alert(status);
          alert(error);
      }
  });
});


I tried adding async: false - in this case, nothing was displayed on the screen at all, but the success block still did not work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vlad Lopushenko, 2018-08-30
@vald5116

In order to help you need to understand what url1 sends

S
Sloth George, 2018-09-05
@dima1994boo

do not use

alert(xhr.ResponseText);
alert(xhr.status);
alert(status);
alert(error);

use
console.log(xhr.ResponseText);
console.log(xhr.status);
console.log(status);
console.log(error);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question