Take the 2-minute tour ×

I wonder how can I make a cross domain request from a gwt application to a grails action. I know there is a wgt-plugin for grails but the development with this plugin is so slow (because I have to compile after each changes in the gwt-code). So I try to develop the gwt part separately and get the data from the grais application via ajax request. But the gwt application is running on a different server (gwt:port 8888, garils:port 8080). If I try to request the grails action I get this error in the browser:

XMLHttpRequest cannot load http://localhost:8080/MyApp/MyDomain/myAction. 

I try to modify the response header in the grails action:

response.setHeader('Access-Control-Allow-Origin', "");

And if I call the action from a browser in the header I can find:


But is do not solve the problem. What I do wrong?

Thanks in advance,



The solution is JSONP. On the GWT-Site my code looks like this:

private void jsonp() {

JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
String URL = "http://localhost:8080/MyApp/myDomain/myAction";

jsonp.requestObject(URL, new AsyncCallback<JavaScriptObject>() {
    public void onFailure(Throwable caught) {

    public void onSuccess(JavaScriptObject result) {



And the grails code look like this:

def myAction = {
    def results = MyDomain.findAll()
    render "${params.callback}(${results as JSON})"

Thanks to Mic.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Web browsers have a built in security mechanism called Same Origin Policy (SOP) that prevent you to do that.

You can use JSONP, which is embedding you response in a Javascript function that will be called when the file is loaded and injected in your page as a SCRIPT tag.

For instance a response could be something like:

myCallbackFunction('a string parameter', {or:{a:json:'data'}}, ...)

And in your page you have a function:

myCallbackFunction(str, json, ...){...}
share|improve this answer
add comment

To solve this issue simply assign a variable to your json string into the eval

private final native MyJson evalJson(String json) /*-{
  eval('var res = ' + json);
  return res;

For more information, see "XMLHttpRequest cannot load. Origin is not allowed by Access-Control-Allow-Origin".

share|improve this answer
add comment

Your Answer


