Ajax jQuery JSON调用

时间:2014-01-28 11:59:34

标签: jquery ajax json jsonp

如果可能,我需要一些帮助。我有一个JSON文件,我试图从图像文件中获得1个结果,并通过数字e指定哪个图像。 G。第一个 - 这是可能的。我尝试过很多不同的方法,但我在这里做错了。

这是我的尝试:http://jsfiddle.net/garrycalliopix/Z233A/

$(function(){
    $.get('http://sunrise.bluechipholidays.co.uk/webservices/property/images/apikey/demo/propertycode/zinc52/propertycode/proch', function(data){
    var $imagetest = $('#imagetest');
    $(data.data).each(function(i, x){
        $(x.images).each(function(j, k){
            var $img = $('<img></img>');
            $img.attr[1]; //i want the 2nd image returned
            $img.attr('width', '150px');
            $img.attr('height', '75px');
            $img.attr("src", k.url).appendTo("#imagetest");
        });

    });
});
});

[0]是返回的图片编号而不是图片数量:[2]是第3张图片。

1 个答案:

答案 0 :(得分:0)

所以你想要获得第二张图片? 你可以用这个小例子得到第二张图片。

下面的示例返回两张图片,那些图像是您想要的图像还是只想要第一张图像?

请参阅this fiddle

$.get('http://sunrise.bluechipholidays.co.uk/webservices/property/images/apikey/demo/propertycode/zinc52/propertycode/proch', function(data){
        var $imagetest = $('#imagetest');
        var j = 0; // keep a counter to count which element you want to display
        $(data.data).each(function(i, x){ // loop through all elements in data.data
            j++; // add one (j = j + 1)
            if(j == 2){ // choose which one you want to display, 2 is the second iteration of $(data.data).each()
                var $img = $('<img></img>');
                            $img.attr('width', '150px');
                $img.attr('height', '75px');
                $img.attr("src", x.images[2].url).appendTo("#imagetest");
            }
        });
    });