JSON为什么要具有status属性 [英] Why should JSON have a status property

查看:149
本文介绍了JSON为什么要具有status属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一种我发现相当普遍的做法.我什至找到了一个以该名称命名的网页,但我忘记了该名称,因此无法再在Google上找到该页面.

I stumbled over a practice that I found to be quite widespread. I even found a web page that gave this a name, but I forgot the name and am not able to find that page on google anymore.

实践是,来自REST服务的每个JSON响应都应具有以下结构:

The practice is that every JSON response from a REST service should have the following structure:

{
    "status": "ok",
    "data": { ... }
}

或在错误情况下:

{
    "status": "error",
    "message": "Something went wrong"
}

我的问题:在JSON中为什么需要这样的"status"属性有什么意义?我认为这就是HTTP状态代码的用途.

REST使用客户端和服务器之间的HTTP通信方式,例如,应使用"DELETE"动词进行删除.同样,如果找不到资源等,则应使用404.因此,出于这种考虑,应该将所有错误情况正确地编码为HTTP状态.

REST uses the HTTP means of communication between client and server, for example the "DELETE" verb should be used for deleting. In the same way, 404 should be used if a resource is not found, etc. So inline with that thinking, any error cases should be encoded properly in the HTTP status.

是否有特定原因在错误情况下返回HTTP 200状态代码,而在JSON中显示错误?处理响应时,似乎只会使javascript条件分支变得更加复杂.

Are there specific reasons to return a HTTP 200 status code in an error case and have the error in the JSON instead? It just seems to make the javascript conditional branches more complex when processing the response.

我发现某些情况下,状态可能为重定向",以告诉应用程序重定向到某个URL.但是,如果使用了正确的HTTP状态代码,浏览器将免费"执行重定向,从而正确维护了浏览历史记录.

I found some cases where status could be "redirect" to tell the application to redirect to a certain URL. But if the proper HTTP status code was used, the browser would perform the redirection "for free", maintaining the browsing history properly.

我主要描绘了两个可能的答案:

I picture mainly two possible answers from you:

  • 要么有两个吵架社区,每个社区都有自己喜欢的方法(始终使用HTTP状态与永远不使用HTTP状态)
  • 或者我错过了重要的一点,您会告诉我,尽管在某些情况下应该使用HTTP状态,但是在某些特定情况下HTTP状态不合适并且"status" JSON属性起作用.

推荐答案

您在这里混合了两个不同的图层:

You are mixing two different Layers here:

  • HTTP用于建立(高级)连接并传输数据.这样, HTTP状态代码会通知您是否以及如何建立连接或为什么不建立连接.在成功连接后,HTTP请求的主体可以包含任何内容(例如XML,JSON等),因此这些状态代码必须定义一般含义.它不会通知您响应的正确性或类型(例如错误消息或数据).

  • HTTP is for establishing (high-level) connections and transferring data. The HTTP status codes thus informs you if and how the connection was established or why it was not. On a successful connection the body of the HTTP request could then contain anything (e.g. XML, JSON, etc.), thus these status code have to define a general meaning. It does not inform you about the correctness or type (e.g. error message or data) of the response.

使用JSON交换数据时,您当然可以省略status属性,但是,如果您知道JSON是否包含您所请求的对象或仅显示一条错误消息,则对JSON的解析就更容易了.读一个属性.

When using JSON for interchanging data you could certainly omit the status property, however it is easier for you to parse the JSON, if you know if it includes the object you were requesting or an error message by just reading one property.

所以,是的,返回200状态代码并在JSON中具有"status": "error"属性是完全正常的.

So, yes, it is perfectly normal to return a 200 status code and have a "status": "error" property in your JSON.

这篇关于JSON为什么要具有status属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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