假设我通过Ajax提交表单并需要服务器的响应:
这种结构的JSON格式是否有标准或最佳实践?如果是这样,我想坚持下去,而不是提出我自己的惯例。
答案 0 :(得分:4)
OmniTI有一个不错的标准我喜欢并推荐:http://labs.omniti.com/labs/jsend
{
status : "success",
data : {
"posts" : [
{ "id" : 1, "title" : "A blog post", "body" : "Some useful content" },
{ "id" : 2, "title" : "Another blog post", "body" : "More content" },
]
}
}
我通常使用变体:
{
status : "error",
messages : {
"some_field" : "message"
}
}
答案 1 :(得分:2)
Peter Bui得到了这种提案格式:http://paydrotalks.com/posts/45-standard-json-response-for-rails-and-jquery
{
status: "ok|redirect|error",
to: "http://www.redirect-url.com",
html: "<b>Insert html</b>",
message: "Insert some message here"
}
答案 2 :(得分:1)
{
"result": "false",
"fields":
[
{"id": "element1", "name": "element1"},
{"id": "element2", "name": "element2"},
{"id": "element3", "name": "element3"}
]
}
答案 3 :(得分:0)
嗯。我不知道标准,但你可能想做一些像
这样的事情{
"result": "false",
"errors":
[
{"errorCode": "1234", "errorText": "malformed address"},
{"errorCode": "5678", "errorText": "no username"}
]
}