开发者

Javascript: JSON 'for loop'

开发者 https://www.devze.com 2022-12-17 07:21 出处:网络
The following Javascript displays in Safari but not Mobile Safari. Can anyone see any bugs? $(\"#results\").append(data);

The following Javascript displays in Safari but not Mobile Safari. Can anyone see any bugs?

$("#results").append(data);

var songdata = JSON.parse(data);

var i = 0;

for (i开发者_如何学JAVA=0;i<=songdata.total;i++)
{
    alert(i);
    var songhtml = "<ul><li><img src=\"" + songdata.data[i].artwork + "\" /></li><li>" + songdata.data[i].title + "</li><li>" + songdata.data[i].artist + "</li><li>" + songdata.data[i].length + "</li><li>" + songdata.data[i].listen + "</li></ul>";
    $("#results").append(songhtml);

}

Thanks in advance.


JSON.parse is not official Javascript, its not supported in all browsers. That could be your problem, but I don't have mobile safari to test it on.

https://github.com/douglascrockford/JSON-js/blob/master/json2.js

Download this file and

<script type="text/javascript" charset="utf-8" src="/js/JSON2.js"></script>


I have to second MindStalkers comment. Id bet its your use of the non cross browser JSON.parse. In addition to that your loop structure looks suspicious - plus it doesnt make much sense to me to why you are using a standard loop instead of jQuery.each().


The for (i=0;i<=songdata.total;i++) looks suspicious to me. You're starting with zero, but continuing equal to or greater than total, so you're going to loop total + 1 times. (I take it this isn't a JavaScript/JSON array, as you're using total rather than length.)

If total tells you how many entries there are, and if the entries start at 0, then use just <. If the entries start at 1, do that. :-) But if you reference an invalid songdata[i], then songdata[i].artwork will fail because songdata[i] will be undefined.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号