Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX response: sugestions for JSON format?

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 coming up with my own convention.

like image 752
StackOverflowNewbie Avatar asked Oct 22 '10 21:10

StackOverflowNewbie


2 Answers

OmniTI has a decent standard that I like and recommend: 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"
    }
}
like image 58
AL the X Avatar answered Sep 22 '22 12:09

AL the X


Peter Bui's got this proposal format: 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"
}
like image 20
Jafin Avatar answered Sep 22 '22 12:09

Jafin