如何获得jquery ajax错误数据,这是正确的响应方式吗?

时间:2022-01-14 20:17:39

I'm using ajax to submit forms. This is a piece of code I wrote:

我使用ajax提交表单。这是我写的一段代码:

    $.post(this.action, $(this).serialize())
        .done(function(data) {
            form.find(".success-msg").text(data);
        })
        .fail(function() {
            // insert error msg to the form
        })
        .always(function() {
             modal.modal("hide");
        });

How can I get the server response in the fail handler? the .fail() callback does not get the returned data like .done() do..

如何在失败处理程序中获得服务器响应?.fail()回调不会获取返回的数据,如.done() do..。

Second, is it correct to return non 200 (say 40X) status for a form validation errors that occur on the server? Or I should return 200 OK status and somehow distinguish myself between error/success in my own response?

其次,对于服务器上出现的表单验证错误,返回非200(比如40X)状态是否正确?或者我应该返回200个OK状态,然后以某种方式将自己与错误/成功区分开来?

i.e on validation errors I can do this

我。如果验证错误,我可以这样做

return HttpResponse(error_msg)

or,

或者,

return HttpResponseBadRequest(error_msg)

2 个解决方案

#1


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).

关于另一个问题——如果你知道失败的原因——我认为最好是回答200个问题,并在回答中包含一个状态:true/false和其他数据。这样,如果您有其他错误代码(服务器没有响应/服务器错误/等等),您可以使用不同的函数/行为。

#2


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.

您可以始终控制日志xhr。responseJSON确保返回的内容。

#1


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).

关于另一个问题——如果你知道失败的原因——我认为最好是回答200个问题,并在回答中包含一个状态:true/false和其他数据。这样,如果您有其他错误代码(服务器没有响应/服务器错误/等等),您可以使用不同的函数/行为。

#2


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.

您可以始终控制日志xhr。responseJSON确保返回的内容。