Created
July 10, 2018 04:10
-
-
Save xiaoysh8/dabd6724885dd6a4d4e8d4c3afdb7070 to your computer and use it in GitHub Desktop.
laravel error message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Laravel 手动返回错误码 | |
| 设想到一个情景,如果新增数据库时用户提交的数据正确,也就是通过了验证,但是添加数据库时发生错误,比如: | |
| if(!$users->save()){ | |
| //新增数据库时发生错误 | |
| } | |
| 那么需要返回错误信息,这个时候怎么手动呢?找到了一个函数,特此记录,感觉够用了: | |
| $errors=array( | |
| 'message'=>"新增数据库发生错误,请稍后再试" | |
| ); | |
| // $errors 定义返回的错误信息 | |
| // 422 表示返回该信息的状态码 | |
| return new JsonResponse($errors, 422); | |
| 这样在前台就能接受到该错误信息了,如下: | |
| $.ajax({ | |
| success:function(){}, | |
| error:function(){ | |
| // 由于返回422的错误状态码,所以会自动调用ajax的错误函数,不需要人为再手工判断 | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment