JavaScript,JSONP和从跨域读取XML [英] JavaScript, JSONP and reading XML from cross-domain

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

问题描述

在我的JS项目中,我需要从跨域加载数据。 (JavaScript位于域A,数据来自域B)

in my JS project I need to load data from cross-domain. (JavaScript sits on domain A, the data comes from domain B)

我有一个解决方案使用 JSONP ,但我真的需要加载一个XML(普通的XML音乐播放列表)。主要目标是能够加载和解析XML数据,而无需首先将其修改为其他格式(如JSONP)。

I have a solution that uses JSONP but I really need to load an XML instead (ordinary XML music playlist). The main goal is to be able to load and parse the XML data without the need to modify them first to some other format (like JSONP).

完全不可能?

我主要定位主要在iOS上的最新浏览器。

I am targeting mainly the latest browsers mainly on iOS.

谢谢!

PS:可以 easyXDM 任何帮助?或者它与XML无关?

PS: Could easyXDM be of any help? Or it's not relevant to XMLs?

更新:不幸的是我不能使用代理,我真的想问一个直接的解决方案。 >

推荐答案

您可以完全做到这一点,只需让您的域B返回

You can totally do this, just have your domain B return something like

func("<myxml></myxml>");

var someVar = "<myxml></myxml>";

名称 JSONP 与JSON没有任何关系因为它的概念都是关于执行嵌入代码中的数据的JavaScript。

The name JSONP doesn't really have anything to do with JSON specifically since its concept is all about executing JavaScript that has your data embedded in the code.

一旦你的域B完全返回上面的两个表单之一,域A就可以简单地使用它可以通过:

Once your domain B returns exactly one of those 2 forms above, domain A can simply use it either by doing:

<script>
function func(xmlString) {
    alert(xmlString); // you can parse the xmlString with 
                      // jQuery or something else
}
</script>

或如果使用第二个示例:

or if you use the second example:

<script>
alert(someVar);
</script>

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

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