将jQuery.getJSON示例代码转换为Dojo JSONP

时间:2013-08-22 19:38:23

标签: jquery dojo jsonp

http://api.jquery.com/jQuery.getJSON/中的这段代码我想转换为Dojo的JSONP。你如何制作它以产生相同的输出?

<script>
(function() {
var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
    $.getJSON( flickerAPI, {
    tags: "mount rainier",
    tagmode: "any",
    format: "json"
    })
    .done(function( data ) {
        $.each( data.items, function( i, item ) {
            $( "<img/>" ).attr( "src", item.media.m ).appendTo( "#images" );
            if ( i === 3 ) {
                return false;
            }
        });
    });
})();

1 个答案:

答案 0 :(得分:1)

Dojo通过dojo/request/script模块处理JSONP请求。 使用此模块,您的代码变为:

<script>
    require([
        "dojo/request/script",
        "dojo/_base/array",
        "dojo/dom-construct",
        "dojo/ready"
    ], function(script, array, domConstruct, ready){
        ready(function(){
            var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne";
            script.get(flickerAPI, {
                jsonp: "jsoncallback",
                query:{
                    tags: "mount rainier",
                    tagmode: "any",
                    format: "json"
                }
            }).then(function(data){
                array.forEach(data.items, function(item){
                    domConstruct.create("img", {src: item.media.m}, "images");
                });
            });
        });
    });
</script>