-1

我正在尝试从 json 提要中的“农场”值开始构建 url 到 photsets 的拇指。我不明白为什么这不起作用..?

    <!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
function ajax_get_json(){
    var menu = document.getElementById("menu");
    var thumbURL = new XMLHttpRequest();
    thumbURL.open("GET", "http://api.flickr.com/services/rest/?&method=flickr.photosets.getList&api_key=797507e576c902b2ffb0c8494fb368ab&user_id=37003559@N03&format=json&jsoncallback=?", true);
    thumbURL.setRequestHeader("Content-type", "application/json", true);
    thumbURL.onreadystatechange = function() {
        if(thumbURL.readyState == 4 && thumbURL.status == 200) {
            var data = JSON.parse(thumbURL.responseText);
            menu.innerHTML = "";
            for(var obj in data){
                menu.innerHTML += data[obj].photosets.photoset.id+"<hr />";
            }
        }
    }
    thumbURL.send(null);
    menu.innerHTML = "requesting...";
}
</script>
</head>
<body>
<div id="menu"></div>
<script type="text/javascript">ajax_get_json();</script>

</body>
</html>

我错过了什么..?

4

1 回答 1

1
function ajax_get_json(){
    var menu = document.getElementById("menu");
    var thumbURL = new XMLHttpRequest();
    thumbURL.open("GET", "https://api.flickr.com/services/rest/?&method=flickr.photosets.getList&api_key=797507e576c902b2ffb0c8494fb368ab&user_id=37003559@N03&format=json&jsoncallback=process", true);
    thumbURL.onreadystatechange = function() {
        if(thumbURL.readyState == 4 && thumbURL.status == 200) {
            eval(thumbURL.responseText);
        }
    }
    thumbURL.send(null);
    menu.innerHTML = "requesting...";
}

function process(data) {
    var menu = document.getElementById("menu");
    menu.innerHTML = "";
    var sets = data.photosets.photoset;
    var max = sets.length;

    for(var i = 0; i < max; i++) {
        menu.innerHTML += sets[i].id+"<hr />"
    }    
}

http://jsfiddle.net/2BTrQ/10/

于 2013-01-24T14:22:38.527 回答