【发布时间】:2014-05-26 02:28:39
【问题描述】:
我有这个脚本:
<script type="text/javascript">
jQuery(function($) {
var newsList = $('.handsontable');
function updateNews(){
newsList.html("Loading…");
$.ajax({
url: "<?php echo $this->createUrl('data')?>",
cache: false,
success: function(data) {
//alert(data);
newsList.html(JSON.parse(data));
},
});
}
updateNews();
});
返回一个有效的 json:
{
"score": [
{
"player_fullname": "Alex",
"game_id": "78",
"player_id": "1"
},
{
"player_fullname": "George",
"game_id": "78",
"player_id": "2"
},
{
"player_fullname": "Nick",
"game_id": "78",
"player_id": "3"
},
{
"player_fullname": "John",
"game_id": "78",
"player_id": "4"
},
{
"player_fullname": "Steve",
"game_id": "78",
"player_id": "5"
}
]
}
我现在想把它转换成数组。我尝试了 JSON.parse(data),但它没有返回任何内容。我究竟做错了什么?请帮忙。
PS。我用的是Yii,这段代码在一个视图中,我需要把这些数据转换成数组,这样我就可以和handsontable api一起使用了。
【问题讨论】:
-
第一个 console.log(data);并检查它返回的字符串或对象
-
它返回我上面提到的json。
-
那么你不需要这样做 JSON.parse(data);直接使用数据它已经是 JSON obj。
标签: php jquery json handsontable