JavaScript、JSONP 以及跨域读取 XML

发布于 2024-10-31 02:51:29 字数 448 浏览 1 评论 0原文

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

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

完全不可能吗?或者有任何解决方法或黑客吗?

我的目标主要是 iOS 上的最新浏览器。

谢谢!

PS:easyXDM 有什么帮助吗?或者它与 XML 无关?

更新:不幸的是我无法使用代理,我真的在询问一个直接的解决方案。

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

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).

Is it completely impossible? Or are there any workarounds or hacks?

I am targeting mainly the latest browsers mainly on iOS.

Thanks!

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

UPDATE: unfortunately I can not use proxy, I am really asking about a direct solution.

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

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

发布评论

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

评论(4

岁月苍老的讽刺 2024-11-07 02:51:29

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

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

或的

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

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

一旦您的域 B 返回上述两种形式之一,域 A 就可以通过以下方式简单地使用它:

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

或者如果您使用第二个示例:

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

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

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

or

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

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.

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>
傲娇萝莉攻 2024-11-07 02:51:29

通常的解决方案是使用“AJAX 代理”——一个在您的域上运行的简单服务器端脚本,它从其他域获取数据并按原样返回。

最简单的方法是为脚本提供您需要从中获取数据的 URL:

http://example.com/proxy.php?url=http%3A%2F%2Fexample.org%2Fajax%3Fid%3D123http://example.org/ajax?id=123 获取数据,

但是如果您让任何 URL 像这样获取,这可能会被滥用,所以您应该有您的脚本,检查它实际上只从特定的 URL 获取数据。

为了避免必须解析 URL 来检查这一点,您可以专门为您的应用程序编写一个代理,该代理仅访问您需要的特定资源:

http://example.com/proxy.php?id=123 访问http://example.org/ajax?id=123

The usual solution is to have a "AJAX proxy" - a simple server-side script running on your domain, that fetches the data from the other domain and returns it unchanged.

The simplist is to give the script the URL you need the data from:

http://example.com/proxy.php?url=http%3A%2F%2Fexample.org%2Fajax%3Fid%3D123 gets the data from http://example.org/ajax?id=123

This can however be misused if you let any URL be fetched like that, so you should have your script, check that it actually only gets data from a specific URL.

In order to avoid having to parse the URL to check this, you could write a proxy specificly for your app, that only accesses the specific resource you need:

http://example.com/proxy.php?id=123 to access http://example.org/ajax?id=123.

街角迷惘 2024-11-07 02:51:29

如果您有 JSON-P 解决方案,则只需将 XML 作为字符串传递给 JSON-P 回调即可。然后,您可以在 JavaScript 中对变量字符串进行 XML 解析

If you have a JSON-P solution in place, you can just pass the XML to the JSON-P callback as a string. You can then do XML parsing of a variable string in JavaScript

无力看清 2024-11-07 02:51:29

JSONP 的整体思想是响应必须可以作为脚本执行。当然,您可以传回 XML 数据,只要它是有效的 Javascript - 例如,服务器可以将其响应包装在字符串中:

myCallback('<xml><stuff/></xml>')

并且您必须使用 jQuery 解析它:

success: function(data) { 
    var xml = $(data); // now do stuff 
}

这假设您控制其他服务器并且/或者有人有兴趣以这种方式格式化他们的数据。否则,你就不走运了,需要某种代理 - 你也许可以 使用 YQL 执行此操作

The whole idea with JSONP is that the response must be executable as script. So sure, you can pass XML data back, as long as it's valid Javascript - for example, the server could wrap its response in a string:

myCallback('<xml><stuff/></xml>')

and you'd have to parse it with jQuery:

success: function(data) { 
    var xml = $(data); // now do stuff 
}

This assumes that you control the other server and/or someone who does is interested in formatting their data that way. Otherwise, you're out of luck, and need a proxy of some sort - you might be able to do this with YQL.

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