跨域JSONP XML响应 [英] Cross Domain JSONP XML Response

查看:159
本文介绍了跨域JSONP XML响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSONP和外部服务器返回我产生一个XML API跨域请求,下面是我的code:

I am making a api cross domain request using JSONP and the external server returns me result in XML, below is my code:

$.ajax({
    type: "Get",
    url: "http://domain.com/function?Data=1234567890",
    xhrFields: {withCredentials: true},
    dataType: "JSONP text xml",
    contentType: "application/xml",
    cache: false,
    success: function(xml)
    {
    alert($(this).find('ResponseStatus').text());
    }
});

返回我的XML,但随着它会产生一个错误说意外令牌LT;不幸的是停止我处理,我不得到一个警告信息。你知道吗?

it returns me a xml but along with that it generates an error saying "Unexpected token <" which unfortunately stops my processing and i dont get an alert message. Any idea?

最佳

推荐答案

正如上述评论,从JavaScript跨域XML是一个没有没有,除非你有在应用程序控制是随地吐痰的XML,并且可以使用格式化招'傻瓜'的脚本到解析它作为JSON。 如果你能做到这一点,虽然,这个问题将不得不为什么不格式为JSON摆在首位? 所以...选项

As mentioned in the comments above, cross domain xml from javascript is a no-no unless you have control over the application that is spitting out the XML and can use a formatting trick to 'fool' the script into parsing it as JSON. If you can do that though, the question would have to be why not just format as JSON in the first place? So... Options

  1. 从应用程序处理与JSONP格式的输出。假设你不能这样做,你的情况,然后......
  2. 使用本地代理在您的网络服务器。有很多简单的代理的例子在那里在PHP,Python或不具有跨域限制任何其他语言。至于你的页面上的脚本,然后而言是本地的AJAX请求。如果你不能做到这一点,然后...
  3. 一种可能是使用像YQL的中介。 YQL和jQuery可以使很多的这些XML问题消失。当然缺点是,你通过了,你无法​​控制的第三方发送的东西。

事情是这样的:

// find some demo xml - DuckDuckGo is great for this
    var xmlSource = "http://api.duckduckgo.com/?q=StackOverflow&format=xml"

// build the yql query. Could be just a string - I think join makes easier reading
    var yqlURL = [
        "http://query.yahooapis.com/v1/public/yql",
        "?q=" + encodeURIComponent("select * from xml where url='" + xmlSource + "'"),
        "&format=xml&callback=?"
    ].join("");

// Now do the AJAX heavy lifting        
    $.getJSON(yqlURL, function(data){
        xmlContent = $(data.results[0]);
        var Abstract = $(xmlContent).find("Abstract").text();
        console.log(Abstract);
    });

当然,在这个例子中你带回所有的XML数据,并在本地搜索 - 选择有调整的select语句带回你想要的东西。

Of course, in that example you are bringing back all the xml data and searching it locally - the option is there to tune the select statement to bring back just what you want.

希望帮助

这篇关于跨域JSONP XML响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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