Retrieving XML data from a ReST service across domains with Dojo

折月煮酒 提交于 2019-12-31 05:34:06

问题


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

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?


回答1:


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.

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){
    }
});

See: dojo.xhrGet Documentation

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.

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.

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



来源:https://stackoverflow.com/questions/13283905/retrieving-xml-data-from-a-rest-service-across-domains-with-dojo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!