2

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

Sean Thoman
  • 7,429
  • 6
  • 56
  • 103

2 Answers2

3

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.

sebastianf182
  • 9,844
  • 3
  • 34
  • 66
  • Hi sfratini, could you please provide some more info on how to create such a proxy? – YakovL Apr 15 '16 at 08:18
  • Sorry, I was in vacation. I will leave this here since it could be useful for others. The usual limitation with ajax is making requests to servers that do not share the same hostname. This is a security issue. You can solve this by adding access to your script in the server but usually this is not possible. But you can do is make a requests to a servlet and this will actually make the request for you. With Java you do this with a URL and Connection classes in the server. This way you dont block the client browser since you sent another thread with the ajax request to your servlet. – sebastianf182 Apr 30 '16 at 20:08
1

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.

dbrin
  • 15,525
  • 4
  • 56
  • 83