【发布时间】:2013-09-20 18:26:53
【问题描述】:
我从 ajax 调用中得到这个
{"id":"381120774951940096","time":"Posted 0 minutes and 51 seconds ago.",....
如何将这些中的每一个添加到变量、id、时间等中? data.id 不起作用,它显示 undef。
<script>
$(function() {
$.ajax({
type: "POST",
url: "start.php",
cache: false,
success: function(data) {
console.log(data.name);
console.log(data);
}
})
});
</script>
这是我从 start.php 返回的内容
$return = array('id' => $info['id_str'] ,'time' => timeDiff(new DateTime($info['created_at'])), 'name' => $info['user']['name'], 'text' => $info['text'], 'image' => $picture, 'url' => "http://twitter.com/{$info['user']['screen_name']}");
print_r(json_encode($return));
编辑 我在 foreach 循环中有那个 print_r,这就是问题所在。所以我添加了另一个数组并在文件末尾使用了 echo json_decode($array,true)。写这个以防万一,可能会对某人有所帮助。
干杯
【问题讨论】:
-
为什么是 print_r? echo 非常好!
-
我只是习惯了print_r。