Trey
Trey

Reputation: 51

Simple JSON issue?

I honestly think I'm having a brain hiccup or something, but I've been caught on this for a solid half hour. I'm posting using jQuery's .post(), and the response is a JSON object such as this:

{
    "meta": {
        "status": 201,
        "msg": "Created"
    },
    "response": {
        "id": 1111111
    }
}

However, I don't know why I can't target anything in this JSON. Here's what I'm dealing with:

$.post('post.php',function(d){
    alert(d) // Returns the JSON string above
    alert(d.meta.status) // Returns 'undefined' (expecting 201)
})

Help! Thanks :)

Upvotes: 0

Views: 79

Answers (1)

VisioN
VisioN

Reputation: 145368

You can pass json as a dataType:

$.post('post.php',function(d){
    alert(d.meta.status)
}, "json");

EDIT: Otherwise, as @IliaG stated in the comments, post.php can pass the content type via:

header("Content-Type: application/json");

Upvotes: 3

Related Questions