从RSS提要的AJAX请求返回XML [英] Returning XML from an AJAX requestfor RSS Feed

查看:118
本文介绍了从RSS提要的AJAX请求返回XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,我想在该网站上向BBC RSS feed请求以返回最新消息.问题是我得到以下错误:

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:

Uncaught SyntaxError: Unexpected token < 

这是我的代码:

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;
    }

然后我像这样在JS代码中调用它:

I then call this in my JS code like so:

    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

推荐答案

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

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

RSS是XML.由于您的通话希望检索JSON,因此<是回文字符,不是吗?

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

阅读jQuery $ .ajax文档,并查找"dataType"选项:

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

更新

根据您在其他答案中添加的评论,看来您最初的问题是跨域请求.

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

为此的最佳解决方案是从服务器端进行跨域调用(使用ASP.NET C#/VB中的服务器代码或其他任何代码,PHP,Perl,Ruby ...),而jQuery将调用您的服务器处理程序检索该RSS feed,因此不再是跨域请求.

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.

这篇关于从RSS提要的AJAX请求返回XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆