通过Javascript加载跨域xml [英] load cross domain xml by Javascript
问题描述
您好
是否可以从不同于纯JavaScript脚本域的域加载XML文件,而不使用php / asp / jsp / ...脚本作为代理?
类似xmlHttpRequest,但能够管理跨域请求。
Hi
Is it possible to load an XML file from a domain that differs from scripts domain with pure javascript and without using a php/asp/jsp/... script as proxy?
Something like xmlHttpRequest but with ability to manage cross domain requests.
感谢
推荐答案
您可以使用JSONP。我知道名字吮吸,因为它不真正与JSON相关。但这需要你对其他域的控制。您需要将XML包装在函数调用中,或将其分配给javascript变量:
You can use something called JSONP. I know the name sucks, because it's not really related to JSON. But this requires you have control over the other domain. You need to wrap your XML inside a function call, or assign it to a javascript variable:
func('< xml> xml>');
或
var myxml ='< xml>< / xml>';
所以如果你的其他域返回这两种格式之一,请使用HTML中的< script src =http:// otherdomain / yourjsonp>< / script>
语法在JavaScript中加载数据。这是一个有点hacky,但很多人使用它。
So if your other domain returns one of these two formats, you can use the <script src="http://otherdomain/yourjsonp"></script>
syntax in your html to load that data in JavaScript. It's a little hacky but a lot of people use it.
这篇关于通过Javascript加载跨域xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!