Accessing a file in another server from GWT Client side
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 sam开发者_JS百科e 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?
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.
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
intosample.js
to contain the same information in JSON with a callback, and reference it with ascript
tag - Compile your website using the cross-site loader (see Controlling Compiler Output), put your
index.html
atb.com/index.html
, put all the rest of your files ona.com
. Then all your RPC calls can go tob.com
, but this means the user would have to navigate tob.com
instead ofa.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
精彩评论