admin管理员组文章数量:1289390
Say I submit a form via Ajax and need a response from the server:
- Pass/fail indicator
- On fail, a list of validation errors with associated field ids/names, etc
Is there a standard or best practice for the JSON format for such a structure? If so, I'd like to try to stick to it instead of ing up with my own convention.
Say I submit a form via Ajax and need a response from the server:
- Pass/fail indicator
- On fail, a list of validation errors with associated field ids/names, etc
Is there a standard or best practice for the JSON format for such a structure? If so, I'd like to try to stick to it instead of ing up with my own convention.
Share Improve this question asked Oct 22, 2010 at 21:44 StackOverflowNewbieStackOverflowNewbie 40.7k114 gold badges290 silver badges452 bronze badges 1- I have never heard of a standard format. – Ruan Mendes Commented Oct 22, 2010 at 22:12
4 Answers
Reset to default 6OmniTI has a decent standard that I like and remend: http://labs.omniti./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" },
]
}
}
I usually use a variant:
{
status : "error",
messages : {
"some_field" : "message"
}
}
Peter Bui's got this proposal format: http://paydrotalks./posts/45-standard-json-response-for-rails-and-jquery
{
status: "ok|redirect|error",
to: "http://www.redirect-url.",
html: "<b>Insert html</b>",
message: "Insert some message here"
}
{
"result": "false",
"fields":
[
{"id": "element1", "name": "element1"},
{"id": "element2", "name": "element2"},
{"id": "element3", "name": "element3"}
]
}
Hmm. I don't know about a standard, but you might want to just do something like
{
"result": "false",
"errors":
[
{"errorCode": "1234", "errorText": "malformed address"},
{"errorCode": "5678", "errorText": "no username"}
]
}
本文标签: javascriptAJAX response sugestions for JSON formatStack Overflow
版权声明:本文标题:javascript - AJAX response: sugestions for JSON format? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741475733a2380870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论