通过Dojo跨域的ReST服务检索XML数据 [英] Retrieving XML data from a ReST service across domains with Dojo

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

问题描述

我正在为基于浏览器的JavaScript客户端编写一个响应XML的ReST应用程序(所以似乎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 方法将注入< script>从指定的网址。该脚本的数据内容将传递给您的加载函数;因此,内容必须作为Javascript验证。您不能将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网站这是一个死胡同。如果您可以访问发送服务器,您可以使用 Access-Control-Allow-Origin 标头。

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在正确的域上。如果您这样做,请包括良好的检查,以便您的服务器代码不被代理人滥用。

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.

有关更多信息,请参阅以下Stackoverflow对话关于Access-Control-Allow-Origin:

jQuery XML REST访问控制允许来源

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

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

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