0

我试图从 PHP 向 JS 返回一个 json 编码的数组,我之前已经做过很多次了,但现在我遇到了一个奇怪的错误。我成功地获取了数据,它以 chrome 显示数组。但是,如果我指定dataType: 'json'. 如果我删除 dataType 并使用 var parsed = JSON.parse(data);它将进入成功函数,但会引发意外的类型错误。请帮忙。

铬输出:

[
    {
        "fullURL": "https://lh6.googleusercontent.com/--ZKG_L-SA9c/UgqECNqP4II/AAAAAAAAA2I/i5nCa3CvKqM/s912/2010raptor_firstdrive002_opt.jpg",
        "thumbURL": "https://lh6.googleusercontent.com/--ZKG_L-SA9c/UgqECNqP4II/AAAAAAAAA2I/i5nCa3CvKqM/s128-c/2010raptor_firstdrive002_opt.jpg",
        "location": "",
        "caption": "",
        "tags": "",
        "program_instance_id": "a0Ji0000001pPO6EAM"
    },
    {
        "fullURL": "https://lh3.googleusercontent.com/-kyUg7_Rul90/UgqEDIu4DhI/AAAAAAAAA2Q/WF0BAEI7smo/s912/220px-Microchip_PIC24HJ32GP202.jpg",
        "thumbURL": "https://lh3.googleusercontent.com/-kyUg7_Rul90/UgqEDIu4DhI/AAAAAAAAA2Q/WF0BAEI7smo/s128-c/220px-Microchip_PIC24HJ32GP202.jpg",
        "location": "",
        "caption": "",
        "tags": "",
        "program_instance_id": "a0Ji0000001pPO6EAM"
    }
]

PHP

$arr = array();
foreach($photoURLS as $photo)
{
$arr[] = $photo;
}

}
echo json_encode($arr);

JS

$.ajax
({
    async: "false",
    type: 'POST',
    data: {action: 'var1', albumName: 'var2'},
    dataType: 'json',
    url: '/controller/function',
        success: function(data) 
        {
        //alert($.isArray(data));
        $.each(parsed, function(i, index) {
        alert(index.fullURL);
        });
        }
 });
4

3 回答 3

1

因此,我重新编写了代码,并认为此解决方案可能对您有用。

$.ajax({
  async: "false",
  type: 'POST',
  data: {
    action: 'var1',
    albumName: 'var2'
  },
  dataType: 'json',
  url: '/controller/function',
  success: function(data) {
    $.each(data, function(index, element) {
      console.log(index);
      console.log(element.fullURL);
      console.log(element);
    });
  }
});

我无法测试 ajax 事件,但是我已经测试了您为每个循环提供的 json 并且它可以正常工作。链接到小提琴

var data = [{
    "caption": "",
        "fullURL": "https://lh6.googleusercontent.com/--ZKG_L-SA9c/UgqECNqP4II/AAAAAAAAA2I/i5nCa3CvKqM/s912/2010raptor_firstdrive002_opt.jpg",
        "location": "",
        "program_instance_id": "a0Ji0000001pPO6EAM",
        "tags": "",
        "thumbURL": "https://lh6.googleusercontent.com/--ZKG_L-SA9c/UgqECNqP4II/AAAAAAAAA2I/i5nCa3CvKqM/s128-c/2010raptor_firstdrive002_opt.jpg"
}, {
    "caption": "",
        "fullURL": "https://lh3.googleusercontent.com/-kyUg7_Rul90/UgqEDIu4DhI/AAAAAAAAA2Q/WF0BAEI7smo/s912/220px-Microchip_PIC24HJ32GP202.jpg",
        "location": "",
        "program_instance_id": "a0Ji0000001pPO6EAM",
        "tags": "",
        "thumbURL": "https://lh3.googleusercontent.com/-kyUg7_Rul90/UgqEDIu4DhI/AAAAAAAAA2Q/WF0BAEI7smo/s128-c/220px-Microchip_PIC24HJ32GP202.jpg"
}];

$.each(data, function (index, element) {
    console.log(index);
    console.log(element.fullURL);
});

还有一个好消息是你的 json 是 100% 有效的,所以传回的接缝是正确的。希望这可以帮助

于 2013-08-20T22:23:39.917 回答
0

parsed您的函数中的变量$.each未定义。您应该在回调函数中使用变量data而不是parsedas 。datasuccess

$.each(data, function(i, index) {
    alert(index.fullURL);
    });
于 2013-08-20T22:11:52.753 回答
0

也许您需要在响应中发送正确的 HTTP 标头。见这里:https ://stackoverflow.com/questions/267546/correct-http-header-for-json-file

于 2013-08-20T21:48:01.617 回答