views:

45

answers:

2

I have a file, sample.xml located at one web server. I want to access this file from a GWT application running at another server. I dont want to make RPC calls to the same server serving GWT application and access the required file on server side (like a proxy). I want to access the file directly from client side as my application is going to be hosted as static files in a web server.

Is there a way to do that?

+1  A: 

Sure - you must issue a XHR (XmlHTTPRequest) from the browser, and then parse the data.
In GWT you can do it using the RequestBuilder class (see here).

Please note that some client side restrictions may apply (e.g. Single Origin Policy etc.)

You issue the request (GET or POST - GET in your case) and pass a callback instance.
The instance's onResponseReceived method receives a Response object, which by calling its getText method returns the received contents.

adamk
the 'RequestBuilder' helped. But my requirement is to access a file from another server than the one from which the application code is served. I am getting com.google.gwt.http.client.RequestPermissionException stating same orgin policy restriction.
sarav
If you have access to the file server, you could ease the SOP restrictions by sending the `Access-Control-Allow-Origin` HTTP header - but it should be used carefully as it does impose some risk.
adamk
A: 

You're trying to have your website (a.com/index.html) reference b.com/sample.xml. I see a few options.

If you have access to b.com's servers:

  • Edit sample.xml into sample.js to contain the same information in JSON with a callback, and reference it with a script tag
  • Compile your website using the cross-site loader (see Controlling Compiler Output), put your index.html at b.com/index.html, put all the rest of your files on a.com. Then all your RPC calls can go to b.com, but this means the user would have to navigate to b.com instead of a.com

If you don't have access to b.com's servers: - Simply provide a link for people to download sample.xml - Host a.com on a server with some kind of script support (PHP, Python, Ruby, Java, anything) and put a proxy to b.com/sample.xml

Steve Armstrong