且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 5阿贾克斯后

更新时间:2023-02-25 17:33:38

我其实只是用这种挣扎自己,答案是pretty的简单实际。

I was actually just struggling with this myself, and the answer is pretty simple actually.

由于Laravel的请求以422状态code,jQuery的成功/完成的功能不火,而误差函数,看到它不是200。

Because Laravel's request responds with a status code of 422, jQuery's success/done functions don't fire, but rather the error function, seeing as it's not 200.

因此​​,为了得到从Request对象中产生,由于验证失败的AJAX请求的JSON响应,您需要定义错误处理程序,在您的情况如下:

So, in order to get the JSON response from your AJAX request generated from the Request object due to validation failing, you need to define the error handler, in your case as follows:

$.ajax({ /* ... */ })
    .done(function(response) { /* ... */ })
    .error(function(data) { // the data parameter here is a jqXHR instance
        var errors = data.responseJSON;
        console.log('server errors',errors);
    });