StackOverflowNewbie
StackOverflowNewbie

Reputation: 40653

AJAX response: sugestions for JSON format?

Say I submit a form via Ajax and need a response from the server:

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.

Upvotes: 5

Views: 3574

Answers (4)

AL the X
AL the X

Reputation: 1051

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"
    }
}

Upvotes: 6

Gopherkhan
Gopherkhan

Reputation: 4342

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"}
        ]
}

Upvotes: 0

Jafin
Jafin

Reputation: 4381

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"
}

Upvotes: 2

Marko
Marko

Reputation: 72230

{
    "result": "false", 
    "fields":
        [
             {"id": "element1", "name": "element1"},
             {"id": "element2", "name": "element2"},
             {"id": "element3", "name": "element3"}
        ]
}

Upvotes: 1

Related Questions