从REST服务检索XML数据跨域使用Dojo [英] Retrieving XML data from a ReST service across domains with Dojo

查看:90
本文介绍了从REST服务检索XML数据跨域使用Dojo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个基于浏览器的JavaScript客户端的REST应用程序,响应XML(这样看来JSONP是出了问题)。

I am trying to write a browser-based Javascript client for a ReST application which responds with XML (so it seems JSONP is out of the questions).

我试图检索使用 dojo.io.script.get 的数据,但传递给回调函数的参数是一个对象,它似乎我不能检索响应的XML数据。

I am trying to retrieve the data using dojo.io.script.get but the parameter that is passed to the callback function is an object from which it seems I cannot retrieve the XML data of the response.

dojo.io.script.get({url:"http://enterpriseapp.enterprisedomain/path/to/rest/collection",
    load:function (data) {
        // 'data' does not contain the actual response (which is XML)
    }
});

什么是检索此数据的正确方法是什么?

What is the correct way to retrieve this data?

推荐答案

dojo.io.script.get 方式将注入A<脚本>从指定的Web地址。从这个脚本中的数据内容将被传递到您的负载功能;因此,内容必须是有效的Java脚本。你不能加载XML到脚本标记。

The dojo.io.script.get method will inject a <script> from the specified web address. The data content from this script will be passed to your load function; hence, the content must validate as Javascript. You can't load XML into a script tag.

如果要加载XML,你需要使用 dojo.xhrGet ;但是,这不会允许请求的第三方网址。使用的好处 dojo.io.script.get 是,你可以使用不同的起始地址比页面加载它们。

If you want to load XML, you'll need to use dojo.xhrGet; however, this will not allow requests to 3rd party urls. The advantage of using dojo.io.script.get is that you can use different origin address' than the page loading them.

dojo.xhrGet({
    handleAs: "xml",
    load: function(dom){
        // do something with the DOM XML object
    },
    error: function(error){
    }
});

请参阅: dojo.xhrGet文档

如果您正试图从另一个网站加载XML它是一个有点穷途末路的。您可以使用访问控制 - 允许 - 原产地头,如果你有机会到发送服务器。

If you are trying to load the XML from another website it's a bit of a dead-end. You can use the Access-Control-Allow-Origin header if you have access to the sending server.

这是我用的另一种解决方案是编写一个代理脚本(在PHP或其他服务器语言)来反映XML在正确的领域。你要小心,如果你这样做是为了有良好的检查,这样你的服务器code不被某人代理滥用。

Another solution that I have used is to write a proxy script (in PHP or other server language) to mirror the XML on the correct domain. You'll need to be careful if you do this to include good checks so that your server code is not abused by someone for proxying.

有关更多信息访问控制 - 允许 - 产地参见下面的#1的对话:
jQuery的XML REST访问控制 - 允许 - 产地

See the following Stackoverflow conversation for more about Access-Control-Allow-Origin:
jQuery XML REST Access-Control-Allow-Origin

这篇关于从REST服务检索XML数据跨域使用Dojo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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