自定义http代码
我可以使用自定义 HTTP 代码吗?
我想使用这些代码作为 AJAX 请求的响应。
示例:
220 - 将对应于某些项目已成功创建的状态 420 - 将对应于发生某些验证错误的状态
每个响应将具有 json 字符串。
Could I use custom HTTP codes?
I want to use these codes as response for AJAX requests.
Example:
220 - will be correspond to status that some item was created successfully
420 - will be correspond to status that some validations errors were occurred
Each response will be has json string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以定义扩展代码,但只有当您想要标准化某些东西时才有意义;在这种情况下,您需要编写规范,并注册状态代码(请参阅 http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p2-semantics-12.html#rfc.section.4.1)。
如果这只是在您的服务器和客户端之间,只需将附加信息放入响应正文中并使用更通用的状态代码即可。
话虽如此,您所说的“420”已经被定义为“422 无法处理的实体”。
You can define extension codes, but it only makes sense if you want to standardize something; in which case you need to write a spec, and get the status code registered (see http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p2-semantics-12.html#rfc.section.4.1).
If this is just between your server and your client, simply put the additional information into the response body and use a more generic status code.
That being said -- what you called "420" is already defined as "422 Unprocessable Entity".
使用您选择的服务器端语言,您可以将标头以及相关的 HTTP 代码和消息发送到浏览器。
Using your server side language of choice you can send headers to the browser with the relevant HTTP code and message.