[Answered ]-How to get jquery ajax error data, and is it the correct way to respond?

1👍

In the done part you have:

.done(function(data) {
    form.find(".success-msg").text(data);
});

But in the fail you didn’t include the data argument. You should include it if you want to use it:

.fail(function(data) {
    // insert error msg to the form
});

Regarding the other question – if you know the reason for the fail – I think it’s better to response with 200 and inside the response to have a status: true/false and the rest of the data. This way you can use different functions/behavior if you got some other error code (server not responding/server error/etc).

👤Dekel

1👍

This might be late but I had this same issue recently and this is my solution.

    .error(function(xhr){
       var result = xhr.responseJSON;
       form.find(".error-message").text(result.data);
     });

You can always console log xhr.responseJSON to be sure what’s being returned.

Leave a comment