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'});";
?>

Leave a comment