未捕获类型错误:undefied 不是 JSON 响应的函数
uncaught type error :undefiened is not a function for JSON response
嗨,我正在使用 json 将歌曲列表发送到我的表。在控制器 url 之前,json 响应正常。但是当我尝试遍历它并将详细信息放在我的表中时,我收到此错误。
$(window).load(function(){
$.get("/MusicPlayer/getSongList", function(data){
var library = data;
listSongs(library);
});
});
function listSongs(library){
var table = $("#table");
var row;
$("#table tbody").remove();
library.forEach(function(song){ ***/*this is the line where there is error. */***
row = $("<tr></tr>");
$("<td />").addClass("song-select").append($("<input
/>").attr({type:"checkbox",class:"checkbox",value:song.title})).appendTo(row);
$("<td>"+song.title+"</td>").appendTo(row);
$("<td>"+song.album+"</td>").appendTo(row);
$("<td>"+song.artist+"</td>").appendTo(row);
$("<td>"+song.rating+"</td>").appendTo(row);
$("<td>"+song.composer+"</td>").appendTo(row);
$("<td>"+song.genre+"</td>").appendTo(row);
row.click(viewFunction());
row.appendTo(table);
});
[
{
"title": "15 - Jacob's Theme.mp3",
"album": "The Twilight Saga - Eclipse",
"artist": "Howard Shore",
"rating": "2",
"composer": "carter ruwell",
"genre": "Soundtrack"
},
{
"title": "07_-_Vennante.mp3",
"album": "Andala Rakshasi (2012)",
"artist": "Ranjith",
"rating": "0",
"composer": "Rathan",
"genre": "TeluguMusic"
},
{
"title": "08. One Simple Idea.mp3",
"album": "Inception (OST)",
"artist": "Hans Zimmer",
"rating": "0",
"composer": "null",
"genre": "?????????"
}
]
我想你需要$.parseJSON
,因为library
应该是一个 JSON 格式的字符串,而不是一个真正的数组。
$.parseJSON(library).forEach(function(song){ .....
json
响应是一个非常特定格式的字符串,但仍然只是一个字符串。
在能够将其用作Javascript数据之前,您需要使用JSON.parse
解析它。
JSON 对象没有为其定义forEach
函数。
用于循环 :
for(var song in library) {
console.log(song, result[song]);
}
或使用 jquery each
$.each(library, function(key, value) {
});
forEach
仅适用于简单数组和支持ECMAScript5
的环境中。也看看这个:在 JavaScript 中的数组上 for-each?
相关文章:
- 从JSON填充列表(而不是HTML)
- 处理一个JSON文件;完全相同的副本不是
- http请求使用html而不是json进行响应
- Sencha Ext JS排序标准以字符串而不是JSON的形式发送
- 按数字而不是按名称选择JSON对象
- 点击浏览器的“后退”按钮显示的是JSON而不是HTML(使用Rails和d3.js)
- 在数据表中使用 JSON 对象而不是数组
- 我怎么能在IONIC应用程序中使用Json对象而不是硬编码对象呢
- '对象不是函数'当使用angular连接两个json数组时
- 主干:在变量中提供JSON,而不是通过URL获取
- 在JSON中创建一个数据对(不是字典,而是两个独立的数据)
- 从aspx.cs页面获取jquery post中的Null响应而不是json
- 将 json 发送到(不是 REST)WCF 服务
- eval() 在解析 json 对象时不是强制性的吗?
- 在使用Protractor进行测试时,我可以用json文件替换结果,而不是调用实际的REST端点
- uncauctTypeError:undefined在使用名为的JSON对象时不是函数
- 快递邮件接收json不是我发送的
- 如何将PHP对象序列化为JavaScript对象(不是JSON)
- ajax返回空字符串,而不是json[python-cgi]
- 如何使用自定义JSON(不是GeoJSON)与Highcharts地图