同源策略、Javascript/jQuery AJAX 和检索 RSS XML 提要

发布于 2024-10-12 09:05:57 字数 116 浏览 7 评论 0原文

我在使用 jQuery 检索位于外部域的 RSS 提要时遇到了问题。它在 Safari 中工作,但由于同源策略限制(也有关于 $.ajax() 函数的文档),其他浏览器会出错。

想知道我是怎么修好的吗?

I came across a problem using jQuery to retrieve an RSS feed located on an external domain. It was working in Safari but other browsers would error because of Same Origin Policy restrictions (which are also documented about the $.ajax() function).

Wanna know how I fixed it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九八野马 2024-10-19 09:05:57

有三种方法可以绕过同源策略:

  1. 代理——就像 Strawberry Sheurbert 所做的那样,非常有效,但浪费了带宽和计算能力
  2. JSONP -- 通过 script 标签加载数据。需要源网站的配合,而且基本上是黑客和笨拙的。
  3. CORS——“正确”的方式,优雅而细致,但需要来自源网站的大量合作,并且不适用于较旧的浏览器。

你付了钱,你就抓住机会了。

There are three ways to get around the Same-Origin Policy:

  1. Proxy -- as Strawberry Sheurbert did, perfectly effective but a waste of bandwidth and computing power
  2. JSONP -- loading the data through the script tag. Needs cooperation from source website and basically hackish and clumsy.
  3. CORS -- the "right" way, elegant and nuanced, but needs a lot of cooperation from source website and doesn't work with older browsers.

You pays your money and you takes your chance.

花间憩 2024-10-19 09:05:57

我制作了一个简单的 PHP 脚本,如下所示:

<?php

/*
    fetch.php fixes this issue: http://en.wikipedia.org/wiki/Same_origin_policy

    Read more:
        *   http://api.jquery.com/jQuery.ajax/
        *   http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin
        *   http://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains
*/

// Requires URL
if ( !isset($_REQUEST['url']) || empty($_REQUEST['url']) ) exit( 'No url specified' );

// Set content-type
$type = 'application/rss+xml; charset=utf-8;';
if ( isset($_REQUEST['type']) && !empty($_REQUEST['type']) ) {
    $type = urldecode($_REQUEST['type']);
}

// Adapted from http://www.howtogeek.com/howto/programming/php-get-the-contents-of-a-web-page-rss-feed-or-xml-file-into-a-string-variable/
function get_url_contents( $url ){
    if ( function_exists('curl_init') ) {
        $crl = curl_init();
        $timeout = 5;
        curl_setopt ($crl, CURLOPT_URL, $url);
        curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
        $ret = curl_exec($crl);
        curl_close($crl);
        return $ret;
    } else {
        return file_get_contents( $url );
    }
    return 'Could not retrieve url';
}

// Output content from url
header( 'Content-type: ' . $type );
echo get_url_contents( urldecode($_REQUEST['url']) );


?>

它看起来很垃圾,但目前运行得很好。我希望它有帮助。

I made a simple PHP script like so:

<?php

/*
    fetch.php fixes this issue: http://en.wikipedia.org/wiki/Same_origin_policy

    Read more:
        *   http://api.jquery.com/jQuery.ajax/
        *   http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin
        *   http://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains
*/

// Requires URL
if ( !isset($_REQUEST['url']) || empty($_REQUEST['url']) ) exit( 'No url specified' );

// Set content-type
$type = 'application/rss+xml; charset=utf-8;';
if ( isset($_REQUEST['type']) && !empty($_REQUEST['type']) ) {
    $type = urldecode($_REQUEST['type']);
}

// Adapted from http://www.howtogeek.com/howto/programming/php-get-the-contents-of-a-web-page-rss-feed-or-xml-file-into-a-string-variable/
function get_url_contents( $url ){
    if ( function_exists('curl_init') ) {
        $crl = curl_init();
        $timeout = 5;
        curl_setopt ($crl, CURLOPT_URL, $url);
        curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
        $ret = curl_exec($crl);
        curl_close($crl);
        return $ret;
    } else {
        return file_get_contents( $url );
    }
    return 'Could not retrieve url';
}

// Output content from url
header( 'Content-type: ' . $type );
echo get_url_contents( urldecode($_REQUEST['url']) );


?>

It's pretty rubbish looking, but it works well enough for the moment. I hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文