AJAX对JSON数据的响应

时间:2018-08-17 03:47:44

标签: php jquery json ajax

我有一个JSON响应,并且格式令人沮丧:

{
   "SearchResults": [
      {
         "SearchItem": "409885340",
         "Shipment": {
            "DRAvail": true,
            "ProNumber": "409885340",
            "PickupNumber": "24861628",
            "CustomerNumber": "688926",
            "BOLNumber": "100982156",
            "BOLReceived": true,
            "PODReceived": false,

等等,依此类推。现在,当我在AJAX中返回成功时(如下):

success: function(data) {
        alert(data);
        alert(JSON.parse(data.SearchResults[0]));

警报数据返回:

{"SearchResults":[{"SearchItem":"409885340","Shipment":{"DRAvail":true,"ProNumber":"409885340","PickupNumber":"24861628","CustomerNumber":"688926","BOLNumber":"100982156","BOLReceived":true,"PODReceived":false,"PONumber":"04272018"...

但是在下一行(解析位置),它会在控制台中返回此值:

  

TypeError:data.SearchResults未定义

我知道我可能会很累,但是我非常感谢您为使它正常工作而提供的帮助...

谢谢

2 个答案:

答案 0 :(得分:1)

您需要先解析所需的get then元素:

//mind the closing parenthesis after data
alert(JSON.parse(data).SearchResults[0]);

答案 1 :(得分:0)

如果您的Ajax响应类型为application / json,则无需解析

alert(data.SearchResults[0]);

会做

其他

如果返回数据是字符串,则需要对其进行解析并获取值

var parsedData = JSON.parse(data);
alert(parsedData.SearchResults[0]);