使用jquery的Ajax适用于IE而不适用于Firefox

时间:2013-08-23 19:39:39

标签: javascript ajax jquery rss

我的最终目标是拥有一个主页,可以提取RSS源并将其显示在我的个人网站的主页上。这一切都是从客户端完成的,我不打算构建一个服务器站点脚本来执行此操作(我会尽管没有其他办法可行)。

以下是页面加载代码。 (我不认为这是一个问题区域,但很高兴看到起点)

 $(document).ready(function () { // Here
    loadpageF(); //<- problems is in this function 
    GetBackGround(document.body); //this just messes with the css of the site
});

function loadpageF() {
    addTableFeed("http://www.npr.org/rss/rss.php?id=1001");
    addTableFeed("http://news.yahoo.com/rss/odd");
    addTableFeed("http://www.nfl.com/rss/rsslanding?searchString=team&abbr=DAL");
    addTableFeed("http://www.npr.org/rss/rss.php?id=1007");


    $(".MainArea").css("width",$("iframe").length * (270));

}
//This function sets up where the RSS data will be place after formatting
 function addTableFeed(feedlink) {

    var numOfCell = maintb.getElementsByTagName("iframe").length;


    maintb.innerHTML += "<iframe id='f" + numOfCell + "' class='iframeFeed'></iframe>";

    getRSSFeed(feedlink, "f" + numOfCell);

}

此代码基本上为每个要添加的ifs和iframe调用一个函数,并将iframe id和URL传递给另一个函数(如下所示,我肯定是99%是问题)获取数据。

 function getRSSFeed(feedlink, iframeid) {


     /*var xmlhttp; old way I did it below realizing it did not work in firefox

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
   xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            var hold = xmlhttp.responseText
            loadData(hold, iframeid);
        }
    }
    xmlhttp.open("get", feedlink, false);
    xmlhttp.send();*/ //end of the old way

    //new way to try to use JSON of course failing at my in goal
    $.ajax({
        type: "GET",
        url: feedlink,
        async: true,
        dataType: "text",   

        success: function(data){
            loadData(data, iframeid);
        },
        error: function(data, statusCode) {
            alert("ERROR: "+data.error)
        }

    });}

函数loadData在调用时工作正常,但不会为Yahoo.com和NFL.com提要调用它。相反,我得到如下所示的警报。

    //Note I has to transcribe from dialog box there may be errors
    ERROR: function () { 
    if( list ) {
        // First, we save the current length
        var start = list length;
        (function add( args ) {
            jQuery.each( args, function( _, arg ) {
                ¡f( jQuery.isFunction( arg ) && (!options unique || self.has( arg)
                    list .push( arg);
                else if( arg  && arg.length ) {
                    // Inspect recursively
                    add( arg);
                }
                });
            })( arguments);
            //Do we need to add the callbacks to the
            // current firing batch?
            if (firing ) (
                firingLength = list length;
            //wiIh memory, if we’re not firing then
            // we should call right away
            J else if ( memory ) (
                firingstart = start;
                fire( memory);

以防万一需要,下面是加载数据代码。

function loadData(xml, ifid) {
    var htmlStr;
    var artCount = 0;

    var $xml = $($.parseXML(xml));
    $("#" + ifid)[0].contentDocument.open();
    $("#" + ifid)[0].contentDocument.close();
    $("#" + ifid)[0].contentDocument.write("<head><link rel='Stylesheet' type='text/css' href='" + document.getElementById("StyleSheet").href + "' /></head>");
    //RSS 2.0 handler
  $xml.find("channel item").each(function () {

        var $article = $(this);
        var title = $article.find("title").text();
        var description = $article.find("description").text();
        var link = $article.find("link").text();
        var pubDate = $article.find("pubDate").text();
        htmlStr = "<div id='" + artCount + "Span" + ifid + "' class='ArticalHead'><h3>" + title + "</h3></div>\n";
        htmlStr += "<div class='ArticalBody' id='" + artCount + "Div" + ifid + "'>\n";
        htmlStr += "\t<p>" + description + "</p><a href='" + link + "' target='_blank' > Click Here For more </a>\n";
        htmlStr += "\t<h6>" + pubDate + "</h6><br />\n";
        htmlStr += "</div>\n"


        $("#" + ifid)[0].contentDocument.write(htmlStr);

         $("#" + ifid).contents().find("#" +artCount + "Span" + ifid).click(function () {
             $("#" + ifid).contents().find("#" + $(this).attr("id").replace("Span", "Div")).toggle();
        });
        artCount++;
    });

    //Atom 1.0 handler
    $xml.find("feed entry").each(function () {

        var $article = $(this);
        var title = $article.find("title").text();
        var description = $article.find("summary").text();
        var link = $article.find("link").attr("href");
        var pubDate = $article.find("published").text();
        htmlStr = "<div id='" + artCount + "Span" + ifid + "' class='ArticalHead'><h3>" + title + "</h3></div>\n";
        htmlStr += "<div class='ArticalBody' id='" + artCount + "Div" + ifid + "'>\n";
        htmlStr += "\t<p>" + description + "</p><a href='" + link + "' target='_blank' > Click Here For more </a>\n";
        htmlStr += "\t<h6>" + pubDate + "</h6><br />\n";
        htmlStr += "</div>\n"


        $("#" + ifid)[0].contentDocument.write(htmlStr);

        $("#" + ifid).contents().find("#" + artCount + "Span" + ifid).click(function () {
            $("#" + ifid).contents().find("#" + $(this).attr("id").replace("Span", "Div")).toggle();
        });
        artCount++;
    });
    //$("#" + ifid).contents().find(".ArticalBody").hide();

}

请原谅我有任何拼写错误或拼写错误。如果需要,我也可以发布整个网页。

0 个答案:

没有答案