A.O.
A.O.

Reputation: 3763

AJAX success callback JSON undefined

Here is the JSON returned from my php:

{
   "approvalSource":[
       {"role_name":"role_1","approval_req_id":"3"},                                                   
       {"role_name":"role_2","approval_req_id":"2"}
    ],
   "doc_source":[
       {"document_name":"testDoc","doc_req_id":"2"}
    ]
}

How come alert(JSON.stringify(data.doc_source)); returns undefined?

Upvotes: 0

Views: 134

Answers (2)

Andrew Bryant
Andrew Bryant

Reputation: 529

It would be useful to see the code that you are using to request and parse the JSON, but some thoughts:

I have often found that this problem is caused by the Content-Type header of HTTP request for the JSON. It needs to be something like 'application/json' for some libraries.

Another possibility is that you're using the string returned by the request that you're making rather than parsing it first.

Upvotes: -1

woofmeow
woofmeow

Reputation: 2408

You are accessing the doc_source incorrectly. This would be the right way to access the property.

alert(JSON.stringify(data["doc_source"]));

Upvotes: 2

Related Questions