0

我试图实现这个例子,但是使用 "$.getScript": 并且由于某种原因,它不会工作....

这是我的代码:

function youtubeFeedCallback(data) {
    var s = '';
    s += '<img src="' + data.entry.media$group.media$thumbnail[0].url + '" width="' + data.entry.media$group.media$thumbnail[0].width + '" height="' + data.entry.media$group.media$thumbnail[0].height + '" alt="' + data.entry.media$group.media$thumbnail[0].yt$name + '" align="right"/>';
    s += '<b>Title:</b> ' + data.entry.title.$t + '<br/>';
    s += '<b>Author:</b> ' + data.entry.author[0].name.$t + '<br/>';
    s += '<b>Published:</b> ' + new Date(data.entry.published.$t).toLocaleDateString() + '<br/>';
    s += '<b>Duration:</b> ' + Math.floor(data.entry.media$group.yt$duration.seconds / 60) + ':' + (data.entry.media$group.yt$duration.seconds % 60) + ' (' + data.entry.media$group.yt$duration.seconds + ' seconds)<br/>';
    if (data.entry.gd$rating) {
        s += '<b>Rating:</b> ' + data.entry.gd$rating.average.toFixed(1) + ' out of ' + data.entry.gd$rating.max + ' (' + data.entry.gd$rating.numRaters + ' ratings)<br/>';
    }
    s += '<b>Statistics:</b> ' + data.entry.yt$statistics.favoriteCount + ' favorite(s); ' + data.entry.yt$statistics.viewCount + ' view(s)<br/>';
    s += '<br/>' + data.entry.media$group.media$description.$t.replace(/\n/g, '<br/>') + '<br/>';
    s += '<br/><a href="' + data.entry.media$group.media$player.url + '" target="_blank">Watch on YouTube</a>';
    document.write(s);
}



$(document).ready(function () {
    $.getScript('http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&alt=json-in-script&callback=youtubeFeedCallback', function () {
        alert(1); //This function is placed in jsPlugin.js

    });

});

警报运行良好,我收到警报,在 FireBug 上我确实看到了 GET 方法,但它不会执行“回调”功能。我没有看到它粘贴在我的网站上...

任何想法为什么?!`

4

3 回答 3

2

这是jsonp。

尝试:

$.getJSON('http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&alt=json-in-script&callback=?', youtubeFeedCallback);

工作演示。

于 2012-09-13T06:29:06.060 回答
0

使用.getJSON(). 有关更多信息,请参阅http://api.jquery.com/jQuery.getJSON/并查找 JSONP。

于 2012-09-13T06:27:14.720 回答
0

如果获取的 JavaScript 中有错误,它将无法正确回调。

注意在文档(https://api.jquery.com/jQuery.getScript/)中,第二个参数回调函数称为“成功”

从 jQuery 1.5 开始,您可以使用 .fail() 来解决错误:

$.getScript("path/to/script.js", function() {
    console.log("Script loaded and no errors");
}).fail(function( jqxhr, settings, exception ) {
    console.log("Script loaded but error occurred:");
    console.log(exception);
});
于 2018-08-15T04:29:37.837 回答