为什么会收到422错误代码?

编程入门 行业动态 更新时间:2024-10-26 02:36:40
本文介绍了为什么会收到422错误代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在发出POST请求,但是除了422响应之外什么都没得到.

I am making a POST request, but unable to get anything besides a 422 response.

Vue.js客户端代码:

new Vue({ el: '#app', data: { form: { companyName: '', street: '', city: '', state: '', zip: '', contactName: '', phone: '', email: '', numberOfOffices: 0, numberOfEmployees: 0, } }, methods: { register: function() { this.$http.post('/office-depot-register', this.form).then(function (response) { // success callback console.log(response); }, function (response) { // error callback console.log(response); }); } } });

拉威尔(Laravel)路线:

Route::post('/office-depot-register', ['uses' => 'OfficeDepotController@register', 'as' => 'office-depot-register']);

Laravel控制器:

public function register(Request $request) { $this->validate($request, [ 'companyName' => 'required', // ... ]); // ... }

推荐答案

Laravel允许您在其接受的字段上定义某些验证.如果您未通过这些验证,它将返回HTTP 422 - Unprocessable Entity.在您的特定情况下,您似乎无法通过一个空的骨架对象进行自己的验证测试,因为需要companyName,并且空字符串不能通过所需的验证.

Laravel allows you to define certain validations on fields it accepts. If you fail these validations, it will return HTTP 422 - Unprocessable Entity. In your particular case, it appears that you're failing your own validation tests with an empty skeleton object, since companyName is required, and an empty string does not pass the required validation.

假设其他字段也经过类似验证,则填充的数据对象应该可以解决您的问题.

Assuming the other fields are similarly validated, a populated data object should solve your issue.

data: { form: { companyName: 'Dummy Company', street: '123 Example Street', city: 'Example', state: 'CA', zip: '90210', contactName: 'John Smith', phone: '310-555-0149', email: 'john@example', numberOfOffices: 1, numberOfEmployees: 2, } }

更多推荐

为什么会收到422错误代码?

本文发布于:2023-06-07 22:08:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!