有没有办法只使用JavaScript从另一个域加载XML文件? [英] Is there a way to load a XML file from another domain using just JavaScript?

查看:70
本文介绍了有没有办法只使用JavaScript从另一个域加载XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery有$ .getJSON()函数,用于从其他域加载json文件,如下所示:

jQuery has the $.getJSON() function that I use to load json files from other domains like so:

$.getJSON('http://somesite.com/file.js', function(output) {
   // do stuff with the json data
});

我想知道我是否可以对来自其他域的xml文件做同样的事情,或者我是否必须使用这是一个服务器端语言?

I was wondering if I can do the same with xml files from other domains or do I have to use a server side language for that?

这是我要加载的xml文档:

This is the xml document I would like to load:

http://google.com/complete/search?output=toolbar&q=microsoft

推荐答案

我同意@viyancs,简单地说如果你想获得其他域的xml,就有跨域限制,解决这个问题的方法是创建一个代理,所以请求过程是:

I agree with @viyancs , simply speaking if you want to get xml of other domain, there is a cross-domain-restriction, the way to solve this is create a proxy, so the request process is:


1。使用$ .ajax来请求您的代理(使用您想要访问的真实xml网址)。

1. use $.ajax to request your proxy(with the real xml url you want to access).

2。您的代理会检索xml网址内容。

2. your proxy retrive the xml url content.

3。您的代理将内容返回到$ .ajax调用。

3. your proxy returns the content to your $.ajax call.

有关详细信息,请查看: http://developer.yahoo.com/javascript/howto-proxy.html

For more detail have a look at: http://developer.yahoo.com/javascript/howto-proxy.html

BTW:为什么你不必为JSON做这个?这是一种名为 JSONP 的技术。

BTW: why you dont have to do this for JSON? it is a technique called JSONP.

这篇关于有没有办法只使用JavaScript从另一个域加载XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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