我正在使用Scoreoid平台来实现排行榜。以下是我的JSON响应:
[{
"Player": {
"username": "TestPlayer",
"email": "",
"first_name": "",
"last_name": "",
"platform": ""
},
"Score": {
"score": "23",
"created": "2012-07-13 22:06:46",
"difficulty": "0",
"platform": ""
}
}]
尝试将此JSON格式化为DIV,使用它(也包含我的调用):
$(document).ready(function(){
$.post('scoreoid_proxy.php', {action:'curl_request', method:'getScores', response:'JSON'},
function(data) {
$.each(data.Player, function(i,Player){
content = '<p>' + Player.username + '</p>';
content += '<br/>';
$(content).appendTo("#response");
});
});
});
但是,我收到以下错误:
object is undefined
length = object.length,
任何帮助都会受到赞赏,我确信这是一件很简单的事情。谢谢。
答案 0 :(得分:1)
data
是一个(对象)数组。
$.each(data, function(i, v){
var Player = v.Player;
var content = '<p>' + Player.username + '</p><br/>'
$("#response").append(content);
});
编辑:您需要告诉jQuery这是JSON,并且需要解析它。这可以通过将'json'
作为参数添加到$.post
(在回调之后)来完成。
$.post('scoreoid_proxy.php', {
action: 'curl_request',
method: 'getScores',
response: 'JSON'
}, function (data) {
$.each(data, function (i, v) {
var Player = v.Player;
var content = '<p>' + Player.username + '</p><br/>'
$("#response").append(content);
});
}, 'json');