我的RESTAPI返回JSON.

I'm currently returning text/plain as the MIME type, but it feels funny. Should I be returning application/x-javascript or some other type?

The second question is with regard to the HTTP status code for error conditions. If my REST API is returning an error state, I am returning as JSON

{ result: "fail", errorcode: 1024, errormesg: "That sucked. Try again!" }

HTTP状态代码是否应该保持为200 OK

推荐答案

The JSON spec suggests application/json, and that seems to be supported by the IETF and IANA registry.

On the second question, I think if the message handling fails in some way you should return a structured and standard error response as a JSON message; only if there is a failure to deliver the message to the backend handler for some reason should you consider an HTTP error code.

Update 2014-06-27:客户端(浏览器)只处理200个响应的日子已经过go 了,RESTful API的主流建议是使用适合于响应的HTTP响应代码,2xx用于成功响应(例如,201为PUT创建;204为DELETE创建无内容),4xx和5xx用于所有错误条件,包括来自API本身的错误条件.

Json相关问答推荐

Azure Devops Pipeline:SON字符串变量丢失所有双引号

如何编写MongoDB查询以返回数组数组

JQ-JSON将键转换为对象

在 python 中循环 JSON 数组

将带有::text[]的JSON数组转换未按预期工作

将环境变量值从 yaml 传递到 json

如何使用 Swiftui 判断 JSON 是否缺少键值对

JBuilder中未定义的局部变量或方法json

为什么在我们有 json_encode 时使用 CJSON 编码

json.dumps 打乱了顺序

如何使用 Jackson 重命名 JSON 序列化中的根键

在 JSON.stringify() 的输出中隐藏空值

Jsonpath 与 Jackson 或 Gson

如何向从文件中检索的 JSON 数据添加键值?

如何按键查找特定的 JSON 值?

JSON Schema:验证数字或空值

与classic 规范化表相比,postgres JSON 索引是否足够高效?

MVC JsonResult camelCase 序列化

如果键可能不存在,则从 Python dict 读取

JSON.stringify 向我的 Json 对象添加额外的 \ 和 "" 的问题