Laravel,使用 jquery ajax 返回响应 400 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24917786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:51:13  来源:igfitidea点击:

Laravel, return response 400 error with jquery ajax

phpjqueryajaxlaravel

提问by SoldierCorp

I built a web system months ago using Laravel 4.1 and in my requests I used Response::json(); to return data to jquery ajax.

几个月前我使用 Laravel 4.1 构建了一个网络系统,在我的请求中我使用了 Response::json(); 将数据返回到 jquery ajax。

  • 200 to return a success and catched by .done() of ajax.
  • 400 to return a fail/error and catched by .fail() of ajax.
  • 200 返回成功并被 ajax 的 .done() 捕获。
  • 400 返回失败/错误并被 ajax 的 .fail() 捕获。

And works very good!

而且效果很好!

The problem now, I'm building another web system with Laravel 4.2 and I'm using the same error codes 200 and 400... 200 works fine but 400 not working because for example:

现在的问题是,我正在使用 Laravel 4.2 构建另一个 Web 系统,并且我使用相同的错误代码 200 和 400 ... 200 工作正常但 400 不起作用,因为例如:

Laravel

Laravel

<?php

    public function example()
    {
        if (Input::get('param1') === 'value1') {
            return Response::json(array(
                'msg' => 'Correct!' 
            ), 200);
        }

        return Response::json(array(
            'msg' => 'Incorrect!'
        ), 400);
    }
?>

jQuery(I'm using the same jquery version: 1.10.2)

jQuery(我使用相同的 jquery 版本:1.10.2)

$.ajax({
    url: 'example',
    type: 'post',
    dataType: 'json',
    data: {param1: 'value1'},
})
.done(function(data) {
    console.log(data.msg);
})
.fail(function(data) {
    console.log(data.msg);
})
.always(function() {
    console.log("complete");
});

I both cases, the request return the json correctly but...

我在这两种情况下,请求都正确返回了 json 但是...

When is Correct/200, .done() of ajax displays the message correctly in console but if it's Incorrect/400, .fail() of ajax not display the message but display undefinedand I don't know what happend.

当是 Correct/200 时,ajax 的 .done() 会在控制台中正确显示消息,但如果是 Incorrect/400,ajax 的 .fail() 不显示消息但显示未定义,我不知道发生了什么。

I hope you can help me.

我希望你能帮助我。

Solved

解决了

Check my self-answer.

检查我的自我回答。

回答by SoldierCorp

Solved

解决了

I can receive the json on fail using this:

我可以使用这个在失败时接收 json:

fail(function(jqXHR)) {
      console.log(jqXHR.responseJSON.msg);
}

And works fine!

并且工作正常!

回答by Jeff Lambert

I think your issue is that, according to the jQuery documentation, your data isn't actually sent to the failfunction:

我认为您的问题是,根据jQuery 文档,您的数据实际上并未发送到fail函数:

jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {}); An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.

jqXHR.fail(function(jqXHR, textStatus, errorThrown ) {}); 错误回调选项的替代构造,.fail() 方法替换了已弃用的 .error() 方法。有关实现细节,请参阅 deferred.fail()。

See also this question.

另请参阅此问题

If you want to get around this and still be able to send some data back to the client in the event of failure, a workaround could be to always return a 200 response but include a 'success'parameter indicating whether the operation was successfully completed or not and just handle everything inside of the donecallback.

如果您想解决这个问题并且在发生故障时仍然能够将一些数据发送回客户端,则解决方法可能是始终返回 200 响应,但包含一个'success'指示操作是否成功完成的参数,并且只是处理done回调中的所有内容。