【问题标题】:Callback function in JSONP jQueryJSONP jQuery中的回调函数
【发布时间】:2013-04-13 17:31:16
【问题描述】:

我正在使用一些 jQuery 脚本从 Flickr 和 YouTube 获取 JSON 提要,并在我的 WordPress 网站上从它们生成 HTML 对于 Flickr,我使用

<!-- Script to grab photos from Flickr feed -->
<script type="text/javascript">
// Set variables needed for query
var URL = "http://api.flickr.com/services/feeds/photos_public.gne";
var ID = "<?php echo get_option('of_flickrid') ?>";
var jsonFormat = "&lang=en-us&format=json&jsoncallback=?";

var ajaxURL = URL + "?id=" + ID + jsonFormat;
// Get the last photos of the flickr account, parse it into HTML code
$.getJSON(ajaxURL, function(data){
     var htmlString = "<h1><?php echo get_option('of_photostext') ?></h1>";

    // Now start cycling through our array of Flickr photo details
    $.each(data.items, function(i,item){

        // I only want the ickle square thumbnails
     var sourceSquare = (item.media.m).replace("_m.jpg", "_s.jpg");

        // Here's where we piece together the HTML
        htmlString += '<a href="' + item.link + '" target="_blank">';
        htmlString += '<img title="' + item.title + '" src="' + sourceSquare;
        htmlString += '" alt="'; htmlString += item.title + '" class="rounded"/>';
        htmlString += '</a>';
        if(i === 11){
            return false;
        }
    });

    // Pop our HTML in the #images DIV
    $('#photos').html(htmlString);

}); // End getJSOON
</script>
<!-- End of Script to grab photos from Flickr feed -->

对于 YouTube:

<!-- Script to grab videos from YouTube feed -->
<script type="text/javascript">
// Set variables needed for query
var URL = "https://gdata.youtube.com/feeds/api/users/";
var UserName = "<?php echo get_option('of_youtubeid') ?>";
var jsonFormat = "/uploads?v=2&alt=jsonc&max-results=2&jsoncallback=?";
// Construct the JSON feed of the YouTube user's channel
var ajaxURL = URL + UserName + jsonFormat;
// Get the last videos of the YouTube account, parse it into HTML code
$.getJSON(ajaxURL, function(data){
     var htmlString = "<h1><?php echo get_option('of_videostext') ?></h1>";

    $.each(data.data.items, function(i,item){       
        // Here's where we piece together the HTML
        htmlString += '<iframe class="videos" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/';
        htmlString += item.id;
        htmlString += '?autoplay=0" frameborder="0"></iframe>';
    });

    // Pop our HTML in the #videos DIV
    $('#videos').html(htmlString);
});
</script>
<!-- End of Script to grab videos from YouTube feed -->

脚本在除 Opera 之外的所有浏览器中运行良好。

当我从 YouTube JSON 链接中删除回调函数时,它可以在 Opera 上运行,但在 IE 上停止,反之亦然。这到底是怎么回事?

【问题讨论】:

    标签: javascript jquery json wordpress opera


    【解决方案1】:

    对于 jQuery AJAX 调用,您不需要将查询字符串附加到您的 URL。使用数据参数described in the documentation。这是一个例子:

    $.getJSON('myURL.php', { param1: value1, param2: value2 }, function(data) {
        //handle my data here
        alert(data);
    });
    

    对于您的情况,我会为 Flickr 尝试以下代码:

    <!-- Script to grab photos from Flickr feed -->
    <script type="text/javascript">
    // Set variables needed for query
    var URL = "http://api.flickr.com/services/feeds/photos_public.gne";
    var ID = "<?php echo get_option('of_flickrid') ?>";
    
    // Get the last photos of the flickr account, parse it into HTML code
    $.getJSON(URL + "?id=" + ID, { lang: 'en-us', format: 'json', jsoncallback: '?' } function(data){
         var htmlString = "<h1><?php echo get_option('of_photostext') ?></h1>";
    
        // Now start cycling through our array of Flickr photo details
        $.each(data.items, function(i,item){
    
            // I only want the ickle square thumbnails
         var sourceSquare = (item.media.m).replace("_m.jpg", "_s.jpg");
    
            // Here's where we piece together the HTML
            htmlString += '<a href="' + item.link + '" target="_blank">';
            htmlString += '<img title="' + item.title + '" src="' + sourceSquare;
            htmlString += '" alt="'; htmlString += item.title + '" class="rounded"/>';
            htmlString += '</a>';
            if(i === 11){
                return false;
            }
        });
    
        // Pop our HTML in the #images DIV
        $('#photos').html(htmlString);
    
    }); // End getJSOON
    </script>
    <!-- End of Script to grab photos from Flickr feed -->
    

    对于 YouTube:

    <!-- Script to grab videos from YouTube feed -->
    <script type="text/javascript">
    // Set variables needed for query
    var URL = "https://gdata.youtube.com/feeds/api/users/";
    var UserName = "<?php echo get_option('of_youtubeid') ?>";
    
    // Get the last videos of the YouTube account, parse it into HTML code
    $.getJSON(URL + UserName + "/uploads", { v: 2, alt: 'jsonc', max-results: 2, jsoncallback: '?' } function(data){
         var htmlString = "<h1><?php echo get_option('of_videostext') ?></h1>";
    
        $.each(data.data.items, function(i,item){       
            // Here's where we piece together the HTML
            htmlString += '<iframe class="videos" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/';
            htmlString += item.id;
            htmlString += '?autoplay=0" frameborder="0"></iframe>';
        });
    
        // Pop our HTML in the #videos DIV
        $('#videos').html(htmlString);
    });
    </script>
    <!-- End of Script to grab videos from YouTube feed -->
    

    【讨论】:

    • 这是交易@hamama:有件事告诉我,如果您的代码不能仅在一个浏览器中运行,那么您会遇到更大的问题。所有浏览器都以相对相同的方式处理代码,因为有些东西在任何地方都可以使用,但有一个地方告诉我你编写的其他东西让 Opera 吓坏了。
    • 更不用说,我没有你所有的代码,所以很难理解上下文。你将不得不自己做一些返工。不过,我只是想向您介绍基本概念。
    • 我无法弄清楚.. Opera 上的脚本工作正常,当我使用 html 发出警报时,即使在 Opera 中也能获得 html 代码!问题是没有附加代码。这是一个小提琴jsfiddle.net/7amama/XCQQn/1,flickr 代码也是如此
    • 那是因为你甚至没有使用我的代码......你仍然使用查询字符串而不是数据参数!
    猜你喜欢
    • 2014-04-06
    • 2014-07-03
    • 1970-01-01
    • 2013-04-29
    • 2019-12-07
    • 2013-05-10
    • 2013-10-17
    • 1970-01-01
    相关资源
    最近更新 更多