Make a Cross-Domain request for XML from a local file

空扰寡人 提交于 2019-12-02 02:05:58

问题


I am not sure if this is even possible. Basically I want to load a local html file on a client PC and have it make a request to a remote server. The data served up by the server is XML.

When I say I am loading a file, I mean the URL in chrome appears as "file:///E:/..."

This is the closest I have gotten to being able to load the XML. I inspected the network tab on the client end and its successfully loading, I just cant seem to get the XML into an element I can inspect:

 var script = document.createElement('script');

 script.setAttribute('src', 'http://xxx.xx.xx.xxx:xxxx/myxmldata');

 script.setAttribute('type', 'text/xml');

 script.setAttribute('id', 'myxml');

 document.getElementsByTagName('head')[0].appendChild(script); 

 var content = document.getElementById("myxml").responseText;// anything I can do here?

 console.log(content);

An AJAX solution would work too. I didn't have any luck with JSONP (this isn't JSON, though).


回答1:


Well, if you are having a problem with the cross domain policy, you might need to build some sort of proxy that will do the request for you. (Its pretty simple to make)

If you want to open a JavaScript file to make an Ajax request I'd use Dojo to parse the XML.

You have a nice example here: http://dojotoolkit.org/reference-guide/dojo/xhrGet.html

Hope it helps.




回答2:


Regardless of payload type JSON or XML what you are doing is JSONP and the result is a javascript function call. So the response must be a valid javascript function call with XML data as input to that function.



来源:https://stackoverflow.com/questions/8829223/make-a-cross-domain-request-for-xml-from-a-local-file

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