简体   繁体   中英

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?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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