Share Coding

Tutorials, Problems, Stuffs …

Cross Domain Request on GWT without RPC

Since RequestBuilder do not support cross domain on GWT, the other option will be JsonpRequestBuilder
Here is an example between client and server (PHP).

 
Client.java

JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
jsonp.requestObject(URL.encode("http://serverurl/Server.php?callback=test_clbk"), new AsyncCallback() {
	public void onFailure(Throwable throwable) {
		System.out.println(throwable.getMessage());
	}
	public void onSuccess(JSONreceiver result) {
		System.out.println(result.getResult());
	}
});

 
JSONreceiver.java

public class JSONreceiver extends JavaScriptObject {
	protected JSONreceiver() {}
	public final native String getResult() /*-{
	    return this.result;
	}-*/;
}

 
Server.php

<?php
	$callback = $_GET['callback'];
	echo "$callback({result: 'Message from Server.php'});";
?>
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: