我应该送一个AJAX请求有何反应/状态code时,有一个用户/表单验证错误? [英] What response/status code should I send to an AJAX request when there is a user/form validation error?

查看:98
本文介绍了我应该送一个AJAX请求有何反应/状态code时,有一个用户/表单验证错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建,用户可以在编辑就地的记录,并通过AJAX提交表单使用jQuery我的web应用程序的功能。

I am creating a feature of my web application where a user can "edit-in-place" a record and submit the form via AJAX using jQuery.

当有人阿贾克斯编辑的纪录,他们用有效的数据提交表单,我送一个200状态code,从而引发了jQuery AJAX成功的功能,则忽略响应主体(因为它是成功的,我并不需要它),并折叠的形式。

When someone is "ajax editing" a record and they submit the form with valid data, I send a 200 status code, which triggers the jQuery AJAX Success function, then ignore the response body (since it was successful, I don't need it), and collapse the form.

当有校验错误,我送一个400状态code,以触发jQuery的错误方法,并在请求的主体我指定的字段不验证。

When there are form validation errors, I send a 400 status code in order to trigger the jQuery error method, and in the body of the request I specify which fields did not validate.

在一个previous StackOverflow上的问题,有人提到,似乎有些奇怪,我是发送400状态code和响应主体工作。是我的方法不是最好的做法?你有什么建议,我做这种情况呢?

In a previous StackOverflow question, someone mentioned that it "seemed odd" that I was sending a 400 status code and working with the response body. Is my approach not a best-practice? What would you recommend that I do in this situation?

推荐答案

使用的4XX状态code中的有效载荷就好了。有没有在HTTP规范说并非如此。

Using the payload of a 4xx status code is just fine. There's nothing in the HTTP spec saying otherwise.

如果你想要的东西比400更具体,看看 422不能处理实体

If you want something more specific than 400, have a look at 422 Unprocessable Entity.

这篇关于我应该送一个AJAX请求有何反应/状态code时,有一个用户/表单验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆