【问题标题】:Passing arrays using json?使用json传递数组?
【发布时间】:2009-05-29 08:40:03
【问题描述】:

我有这个 php 代码

   $ids = array(1,2,3);
   $names = array("cat","elephant","cow"); 
   $originalSettings = array ('ids'=>$ids,'names'=>$names);
   $jsonSettings = json_encode($originalSettings);
   echo $jsonSettings;

这是 jQuery 代码

$.post("ajax.php", {},
function(data){
data.ids.each(function(i) {
alert(data.names[i]);
}
//is it possible to receive the arrays and navigate them   
}, "json");

如何使用 json 传递数组并在 javascript 中接收它们?

谢谢

【问题讨论】:

  • 您的警报后缺少一个括号(调用

标签: php javascript jquery json


【解决方案1】:

试试:

function(data) {
    $.each(data.array1, function(i) {
        alert(data.array2[i]);
    });
}

【讨论】:

  • 谢谢它有效,它略有不同 $.each(data.array1,function(i){alert(data.array2[i]);});
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 2012-12-05
  • 2012-01-26
  • 2016-05-20
  • 1970-01-01
相关资源
最近更新 更多