从rss获取另一个域的图像(然后附加)

时间:2010-11-17 21:47:06

标签: php jquery ajax append

我想从flickr rss获取图像,但是这段代码不会得到它们。 这是我在原始qustion的帮助下得到的:Jquery in Internet Explorer: find image problem(works in FF and Chrome)

问题是以正确的格式将数据传输到我的网页。 我的网页网址是:zalastax.co.cc/pictures.html

使用Javascript:

$.ajax({
    type: "GET",
    url: "js/getflickreasy.php",
    dataType: "xml",
    success: function(data) {
        $(data).find("item").each(function() {

            var item = $(this), title, description, thumbnail;

            title = item.find("title").text();
            description = item.find("description").text();
            thumbnail = item.find("img").attr("src");
        });
    }
});

PHP:

<?
header("content-type: text/xml");
readfile("http://api.flickr.com/services/feeds/photos_public.gne?id=42980910@N02&lang=en-us&format=xml");
?>

编辑:Mathews代码有效 但我在添加一些数据时遇到了问题。

    var currentImage = 0;
//get flicker images from rss.
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=42980910@N02&lang=en-us&format=json&jsoncallback=?", function(data) {
    $(data.items).each(function() {
        var item = this,
            title, description, thumbnail;
        title = item.title;
        description = item.description;
        thumbnail = item.media.m;
        if (currentImage % 3 === 0) {
            $("#apa").append("<p>HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH</p>");
        }
        currentImage++;
        $("#apa").append("<div id=\"div" + currentImage + "\" class=\"imageContainer pictDiv\"><img id=\"img" + currentImage + "\" class=\"bild pictImg\" src=\"" + thumbnail + "\" /></div>");
        $("#bakgrund").append("<img id=\"bkg" + currentImage + "\" class=\"bgrund pictBkg\" src=\"Bilder/polaroid.png\" />");
    });

    $("#bakgrund").append("<img id=\"bkg" + currentImage + "\" class=\"bgrund pictBkg\" src=\"Bilder/polaroid.png\" />");
});

此代码适用于Google Chrome。在Firefox中,它检索数据但不执行追加。 如果我尝试添加其他类似简单文本的内容,Firefox也不会附加。

当我通过jsfiddle.net尝试它时它会起作用,但是当它通过我自己的网页尝试时它没有。

2 个答案:

答案 0 :(得分:1)

您可以在没有PHP的情况下使用JSONP执行此操作:

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=42980910@N02&lang=en-us&format=json&jsoncallback=?", function(data) {
        $(data.items).each(function() {
            var item = this, title, description, thumbnail;
            title = item.title;
            description = item.description;;
            thumbnail = item.media.m;
        });
    });

我制作了一个jsFiddle demo

答案 1 :(得分:0)

http://simplehtmldom.sourceforge.net/

获取网站的简便方法和加载特定数据的简单界面:)