简体   繁体   English

AJAX响应:JSON格式的消息?

[英]AJAX response: sugestions for JSON format?

Say I submit a form via Ajax and need a response from the server: 假设我通过Ajax提交表单并需要服务器的响应:

  • Pass/fail indicator 通过/失败指示器
  • On fail, a list of validation errors with associated field ids/names, etc 失败时,包含相关字段ID /名称等的验证错误列表

Is there a standard or best practice for the JSON format for such a structure? 这种结构的JSON格式是否有标准或最佳实践? If so, I'd like to try to stick to it instead of coming up with my own convention. 如果是这样,我想坚持下去,而不是提出我自己的惯例。

OmniTI has a decent standard that I like and recommend: http://labs.omniti.com/labs/jsend 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" },
        ]
     }
}

I usually use a variant: 我通常使用一个变体:

{
    status : "error",
    messages : {
        "some_field" : "message"
    }
}

Peter Bui's got this proposal format: http://paydrotalks.com/posts/45-standard-json-response-for-rails-and-jquery 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"
}
{
    "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"}
        ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM