【问题标题】:$.get not returning any data in Internet Explorer$.get 在 Internet Explorer 中不返回任何数据
【发布时间】:2011-02-11 00:40:57
【问题描述】:

我正在使用 JQuery 的 $.get 函数来获取 twitter 提要并将其显示在我的网站上。我不知道为什么它似乎没有得到任何数据(即 function(d) { ... } 中的代码没有被调用)。它在我尝试过的所有其他东西中都可以正常工作。我之前也用过这段代码没有问题,我唯一能想到的就是它是通过https运行的。

(请注意,对于示例,我已从提要 url 中删除了 twitter 用户 ID)

JS:

    $.get('proxy.php?url=http://twitter.com/statuses/user_timeline/999999999.rss', function(d) {    
        $(d).find('item').each(function() {
            var theItem = $(this);
            var title = theItem.find('title').text();


            var date = new Date(theItem.find('pubDate').text());
            var alink = theItem.find('link').text();

            // code ommitted (inserts tweet into page)
        });
  });

proxy.php:

<?php
    // PHP Proxy
    // Loads a XML from any location. Used with Flash/Flex apps to bypass security restrictions
    // Author: Paulo Fierro
    // January 29, 2006
    // usage: proxy.php?url=http://mysite.com/myxml.xml

    $session = curl_init($_GET['url']);                    // Open the Curl session
    curl_setopt($session, CURLOPT_HEADER, false);          // Don't return HTTP headers
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);   // Do return the contents of the call
    $xml = curl_exec($session);                            // Make the call

    $seconds_to_cache = 300; // five mins (60 * 5)
    $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
    header("Expires: $ts");
    header("Pragma: cache");
    header("Cache-Control: maxage=$seconds_to_cache");
    //header("Content-Type: text/xml");   // Set the content type appropriately
    header("Content-Type: application/rss+xml");
    echo $xml;        // Spit out the xml
    curl_close($session); // And close the session
?>

任何想法/帮助将不胜感激

【问题讨论】:

  • 它在什么时候中断?该脚本是否设法获取任何数据?你有什么错误吗?
  • 不应该是"Expires: ".$ts"Cache-Control: maxage=".$seconds_to_cache吗?
  • PHP 脚本似乎工作正常,我确实按照您的建议更改了这些变量以确保安全,但是当在地址栏中物理访问 url 时,它会按预期呈现提要,并且可以正常工作在其他浏览器中很好。我没有收到任何 JS 错误,它只是默默地失败。如果我在函数内(即在 $(d).find 行之前)放置警报,则不会显示。

标签: php jquery ajax internet-explorer get


【解决方案1】:

想通了。 IE 需要将内容类型设置为 text/xml。我更改了 proxy.php 脚本:

header("Content-Type: text/xml");

这就是我所需要的。

【讨论】:

  • 我看到了一个类似的问题。你刚刚把那行放在php文件中吗?你是怎么做到的?
【解决方案2】:

我以前遇到过这个问题,你说得对——你主要在 IE 中看到它。这是修复:

$(document).ready(function() {
  $.ajaxSetup ({
    cache: false
  });
});

在您调用 $.get() 之前在您的页面上应用该属性,这很可能会消除该问题。

【讨论】:

    【解决方案3】:

    编辑:

    我想我知道发生了什么。默认情况下,$.get() 方法的返回内容类型为text/html。您正在获取 XML 数据,并立即将其传递给 jQuery 函数进行评估。除 IE 之外的所有浏览器都允许您创建这些 XML 节点,但如果 IE 看​​到它无法识别的元素类型,它将失败。

    您的问题的解决方案似乎是明确地说您期望响应中的 XML。请参阅this example 了解如何进行此操作。

    编辑 2:

    在进一步阅读中,我发现即使使用上面提供的示例,让 IE 正确解释 XML 也是好坏参半。如果您仍然遇到问题,您可以随时尝试在服务器上将您的 XML 转换为 JSON(请参阅 http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/ 以了解完成此操作的工作 class)。您将能够在客户端始终使用 JSON,但您需要相应地更新您的 success 数据处理函数。

    原答案:

    我愿意猜测问题是 IE 很乐意缓存 URL。我建议附加一个额外的、未使用的、但随机生成的查询字符串参数来解决这个问题。

    $.get('proxy.php?url=http://twitter.com/statuses/user_timeline/999999999.rss&rand=' + Math.floor(Math.random()*999999), function() { //omitted });
    

    【讨论】:

    • 谢谢,我试了一下,但结果相同(我在 JS 文件上也使用了相同的技术,因为我很偏执)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 2018-05-04
    • 2015-05-08
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    相关资源
    最近更新 更多