从RSS Feed的AJAX请求返回XML

时间:2021-09-10 00:25:47

I have a website where I'd like to make a request to the BBC RSS feed to return the latest news. The problem is I get the following error:

我有一个网站,我想向BBC RSS提要请求返回最新消息。问题是我收到以下错误:

Uncaught SyntaxError: Unexpected token < 

This is my code:

这是我的代码:

var url = 'http://feeds.bbci.co.uk/news/rss.xml';

$.ajax({
    url : url,
    dataType : 'jsonp',
    contentType : 'text/xml',
    success : function(data) {
        console.log(data);
    }
});

Edit

Here is my code on my server, as suggested by the answers below:

这是我服务器上的代码,如下面的答案所示:

    public XmlDocument callBBCFeed()
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://feeds.bbci.co.uk/news/rss.xml");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream());

        string result = sr.ReadToEnd();

        XmlDocument doc = new XmlDocument();

        doc.LoadXml(result);

        return doc;
    }

I then call this in my JS code like so:

然后我在我的JS代码中调用它,如下所示:

    var url = 'http://myServer/Global/callBBCFeed';



    $.ajax({
        url : url,
        dataType : 'xml',
        success : function(data) {
            console.log(data);
        }
    });

But I get the Same Origin Policy Error

但我得到同源策略错误

2 个解决方案

#1


1  

Check your $.ajax call: dataType: 'jsonp'.

检查$ .ajax调用:dataType:'jsonp'。

RSS is XML. Since your call expects retrieving JSON, < is an ilegal character, isn't it?

RSS是XML。由于你的调用期望检索JSON, <是一个非法字符,不是吗?< p>

Read jQuery $.ajax documentation and look for "dataType" option:

阅读jQuery $ .ajax文档并查找“dataType”选项:

UPDATE

UPDATE

Based on some comment that you added to some other answer, it seems that your initial problem is cross-domain requesting.

根据您添加到其他答案的一些评论,您的初始问题似乎是跨域请求。

Best solution for that is do that cross-domain call from the server-side (using server code in ASP.NET C#/VB or whatever, PHP, Perl, Ruby...) and jQuery will call your server handler to retrieve that RSS feed, so it's not a cross-domain request anymore.

最好的解决方案是从服务器端进行跨域调用(使用ASP.NET C#/ VB中的服务器代码或其他任何东西,PHP,Perl,Ruby ......),jQuery将调用您的服务器处理程序来检索RSS feed,所以它不再是跨域请求。

#2


0  

You are sending a cross domain AJAX request to an XML resource. That cannot work due to the same origin policy restriction.

您正在向XML资源发送跨域AJAX请求。由于相同的原产地政策限制,这不起作用。

You are in a complete contradiction here:

你在这里完全矛盾:

dataType : 'jsonp'

and yet sending a request to a XML resource.

然后向XML资源发送请求。

JSONP which allows for cross domain AJAX calls is something entirely different. It represents a JSON response wrapped in a javascript function that might look like this:

允许跨域AJAX调用的JSONP完全不同。它表示包含在javascript函数中的JSON响应,可能如下所示:

someFunctionName({"foo":"bar", "baz":"bazzy"})

If the remote server doesn't support JSONP, you will have to write a server side script on your domain that will serve as a bridge between yours and the remote domain to fetch the XML file. Then use jQuery AJAX to send a request to your script.

如果远程服务器不支持JSONP,则必须在域上编写服务器端脚本,该脚本将充当您和远程域之间的桥梁以获取XML文件。然后使用jQuery AJAX向您的脚本发送请求。

I would recommend you reading the following jQuery cross domain AJAX guide for different techniques that could be used.

我建议你阅读以下jQuery跨域AJAX指南,了解可以使用的不同技术。

#1


1  

Check your $.ajax call: dataType: 'jsonp'.

检查$ .ajax调用:dataType:'jsonp'。

RSS is XML. Since your call expects retrieving JSON, < is an ilegal character, isn't it?

RSS是XML。由于你的调用期望检索JSON, <是一个非法字符,不是吗?< p>

Read jQuery $.ajax documentation and look for "dataType" option:

阅读jQuery $ .ajax文档并查找“dataType”选项:

UPDATE

UPDATE

Based on some comment that you added to some other answer, it seems that your initial problem is cross-domain requesting.

根据您添加到其他答案的一些评论,您的初始问题似乎是跨域请求。

Best solution for that is do that cross-domain call from the server-side (using server code in ASP.NET C#/VB or whatever, PHP, Perl, Ruby...) and jQuery will call your server handler to retrieve that RSS feed, so it's not a cross-domain request anymore.

最好的解决方案是从服务器端进行跨域调用(使用ASP.NET C#/ VB中的服务器代码或其他任何东西,PHP,Perl,Ruby ......),jQuery将调用您的服务器处理程序来检索RSS feed,所以它不再是跨域请求。

#2


0  

You are sending a cross domain AJAX request to an XML resource. That cannot work due to the same origin policy restriction.

您正在向XML资源发送跨域AJAX请求。由于相同的原产地政策限制,这不起作用。

You are in a complete contradiction here:

你在这里完全矛盾:

dataType : 'jsonp'

and yet sending a request to a XML resource.

然后向XML资源发送请求。

JSONP which allows for cross domain AJAX calls is something entirely different. It represents a JSON response wrapped in a javascript function that might look like this:

允许跨域AJAX调用的JSONP完全不同。它表示包含在javascript函数中的JSON响应,可能如下所示:

someFunctionName({"foo":"bar", "baz":"bazzy"})

If the remote server doesn't support JSONP, you will have to write a server side script on your domain that will serve as a bridge between yours and the remote domain to fetch the XML file. Then use jQuery AJAX to send a request to your script.

如果远程服务器不支持JSONP,则必须在域上编写服务器端脚本,该脚本将充当您和远程域之间的桥梁以获取XML文件。然后使用jQuery AJAX向您的脚本发送请求。

I would recommend you reading the following jQuery cross domain AJAX guide for different techniques that could be used.

我建议你阅读以下jQuery跨域AJAX指南,了解可以使用的不同技术。