简体   繁体   English

AJAX成功回调JSON未定义

[英]AJAX success callback JSON undefined

Here is the JSON returned from my php: 这是从我的php返回的JSON:

{
   "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)); 怎么alert(JSON.stringify(data.doc_source)); returns undefined? 返回未定义?

You are accessing the doc_source incorrectly. 您错误地访问了doc_source 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: 查看用于请求和解析JSON的代码会很有用,但请注意以下几点:

I have often found that this problem is caused by the Content-Type header of HTTP request for the JSON. 我经常发现此问题是由JSON的HTTP请求的Content-Type标头引起的。 It needs to be something like 'application/json' for some libraries. 对于某些库,它必须类似于“ application / json”。

Another possibility is that you're using the string returned by the request that you're making rather than parsing it first. 另一种可能性是您使用的是您提出的请求返回的字符串,而不是先解析它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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