开发者

GWT JSONP with Post not Get

I have a web service in the form `http://....../methodName

It returns a jsonp result such as:

methodNa开发者_高级运维me(["a":"a", "b":"b"]) 

GWT provides JsonpRequestBuilder class to parse jsonp.

  JsonpRequestBuilder rb = new JsonpRequestBuilder();

  rb.setCallbackParam("callback");

  rb.requestObject("http://...../methodName", new AsyncCallback<TestJS>(){
  ...
});

This structure makes a request to url : "http://...../methodName/?callback=__gwt_jsonp_P0.onSuccess".

My web service returns a callback with methodName not with __gwt_json..... So gwt could not create a JavaScriptObject from that response. Also JsonpRequestBuilder works with GET not POST.

How can I achieve those: Sending requests with POST and modifying GWT's default callback name.


JSONP will NOT work with POST. Its not a GWT limitation btw.

JSONP is essentially including a javascript file from your server. So, when you make a JSONP call, a temporary tag is added to the DOM.

Now, a <script> tag can always makes a GET request. That's a browser thing, and GWT cannot do much about it.

If you want to make a cross-domain POST call, you have to chose from one of the following options (and they have nothing to do with GWT)

  • Use Flash plus a crossdomain.xml that allows cross domain posts
  • Use Cross Origin Resource Sharing, or CORS. NOTE that this is only supported in modern browsers
  • Use a proxy server on your domain


Unfortunatly, this isn't how JsonP works. The requests are made by adding a tag to the page, and the results are passed into a function wrapped around the data – in your case, __gwt_jsonp_P0.onSuccess.

The callback name can't be affected, at least while using JsonpRequestBuilder – the system needs to account for the fact that you could send multiple requests out at once, possibly even to different endpoints. A JsonP endpoint that doesn't allow the caller to customize the callback function name is very unusual, and even more odd is an endpoint expecting JsonP calls that expects an impossible POST.

You can implement your own JsonP client side code by using the ScriptElement type, and registering your own global callback to call into your GWT java code.

Look into the API docs for the web service, and see if there is perhaps a better way to communicate with it, perhaps by using a proxy on your own server, avoiding the cross domain issue altogether.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜