Making Cross Site Asynchronous HTTP Post from GWT

2019-07-18 10:20发布

问题:

I m working in a GWT based Project and i'm used to making RPC requests alone. I tried creating a new project and making Cross Site request with GWT client using RequestBuilder, but i couldn't get it to work.

I'm not really good with GWT. So Please can anyone tell me how to create a new Project and its settings along with the code for Making Cross site requests from GWT Client(completely removing RPC calls) ..?

I've went through google and stackoverflow, but no complete thing on this topic. It will be really helpful for every one i hope.

回答1:

If you intend in only supporting browsers that support CORS (which, you'll notice, excludes IE, at least until IE10 is released), then you can simply use RequestBuilder or GWT-RPC, like you always did.
If you ever use custom request headers, then the server will have to allow the client application to do those cross-origin requests, by sending back the appropriate Access-Control-Allow-Origin header in response to OPTIONS requests (known as preflight requests in CORS).

If you want/need to support IE, and you have to make a POST request, then you have no other choice but using a FormPanel and using some trick to pass the response back to the client (the easiest is to redirect to a page on the same origin as the GWT app, passing the result in the query-string).

If you can do GETs instead of POSTs, then you can use the JsonpRequestBuilder (and of course adapt your server code to respond with JavaScript)



回答2:

We can make Cross Site requests by setting "Access Control Allow Origin http://myexample.com" in Response Headers. It will make the response accessible by the browser. More information https://developer.mozilla.org/en/http_access_control