Dingo API 表单验证错误 App 无法处理?

表单验证跑出的422状态码的响应会进到ios常用的网络请求组件的error里,且ios程序员跟我说error里拿不到body里返回的信息,有人有经验这样怎么处理吗?

他用的应该是AFNetworking 这个ios网络库

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 5
liyu001989

照这个逻辑,那么http 422的错误信息应该放在哪?

如果要用github的api,做个app,岂不是完蛋了?

file

7年前 评论

@liyu001989 所为我在想如果ios不能处理,框架肯定会告诉大家应该怎么去写api,但是他说他拿不到我返回的信息,最好有个demo说服他

7年前 评论

你们ios是个菜 不就是一个json字符串?

7年前 评论

@YuxiangDong 422 错误码的含义是请求的内容无法识别,但是在 AFNetworking 里面把这个当作错误响应了。看看这个问题:
https://github.com/AFNetworking/AFNetworki...

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

所以两种方法咯,要不让你的同事重写一个 response serializer 来处理这个响应,要不你就返回一个 AFNetworking 可以获取body的响应了,如果要修改laravel 的表单验证失败响应码,只有重写response 方法,貌似没其他的设置什么的。。。

public function response(array $errors)
    {
        if ($this->ajax() || $this->wantsJson()) {
            return new JsonResponse($errors, 422);
        }

        return $this->redirector->to($this->getRedirectUrl())
                                        ->withInput($this->except($this->dontFlash))
                                        ->withErrors($errors, $this->errorBag);
    }
7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!