从PHP中检索Javascript中的JSON数组 [英] Retrieving JSON array in Javascript from PHP

查看:95
本文介绍了从PHP中检索Javascript中的JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am attempting to return a json encoded array to JS from PHP and i've done so many times before but now i'm getting a weird error. I am successfully getting the data and it's displaying the array in chrome. However, I cannot get it to enter the AJAX success function if I specify the dataType: 'json'. If I remove the dataType and use var parsed = JSON.parse(data); it will enter the success function but it will throw an unexpected type error. Please help.

Chrome输出

[
    {
        "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



PHP

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

}
echo json_encode($arr);






JS




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);
        });
        }
 });


推荐答案

所以我回过头来思考这个解决方案为你工作。

So I worked the code back and think this solution might work for you.

$.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它接缝工作。 LID TO FIDDLE

I can't test the ajax event however I have tested out the json you provided with the each loop and it seams to work. LINK TO FIDDLE

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%有效,所以传回的接缝是正确的。希望这有助于

also good news is that your json is 100% valid so what is being passed back seams correct. Hope this helps

这篇关于从PHP中检索Javascript中的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆