请问n00b
我有这个简单的json jquery调用,我遇到了问题。
<script>
$(document).ready(function(){
var table = $('#datatable');
$.ajax({
type: 'GET',
url: 'http://jsonurl',
dataType: 'jsonp',
success: function(data) {
$.each(data.responseData.leaderboard, function(i, item){
var rowItem = '<tr><td>' + item.userid +
'</td><td>' + item.username +
'</td><td>' + item.companyname +
'</td/></tr>';
table.append(rowItem);
return i<4;
});
}
});
});
</script>
json url返回这样的结果
{"status": "success",
"message": "",
"responseData": {
"leaderboard": [
{
"userid": 1,
"username": "username1",
"companyname": "Working com 1",
"currentlevel": 1
},
{
"userid": 2,
"username": "username2",
"companyname": "Working com 1",
"currentlevel": 13
}
]
}
}
当我尝试使用不在数组中的结构时,代码可以工作,但在上面的代码中却遇到了SyntaxError:missing; Firebug控制台中的before语句错误。我究竟做错了什么?
最佳答案
我可以通过省略变量分配来复制错误,如下所示。
如果您要解释问题的这一部分,将很有帮助...
当我尝试使用不在数组中的结构时,代码有效,但是
与上面我得到一个SyntaxError:missing;
本示例将返回SyntaxError:missing;声明前
<script type="text/javascript">
// var data =
{"status": "success",
"message": "",
"responseData": {
"leaderboard": [
{
"userid": 1,
"username": "username1",
"companyname": "Working com 1",
"currentlevel": 1
},
{
"userid": 2,
"username": "username2",
"companyname": "Working com 1",
"currentlevel": 13
}
]
}
}
</script>
这个例子可以正常工作:
<script type="text/javascript">
var data =
{"status": "success",
"message": "",
"responseData": {
"leaderboard": [
{
"userid": 1,
"username": "username1",
"companyname": "Working com 1",
"currentlevel": 1
},
{
"userid": 2,
"username": "username2",
"companyname": "Working com 1",
"currentlevel": 13
}
]
}
}
</script>
关于javascript - 语法错误:丢失;声明Json数组之前,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30149475/