DJ_
DJ_

Reputation: 105

Timeout exception with JsonpRequestBuilder accessing google web page

so I'm currently trying to use the JsonpRequestBuilder API to try to access a remote file however I'm just testing with the google webpage link and I'm getting a Timeout exception. Not too sure what I'm missing here. Any help would be appreciated.

package com.webapp.client;

import java.util.logging.Logger;

import com.webapp.client.ExcelFileJavascriptObject; import com.google.gwt.jsonp.client.JsonpRequestBuilder; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback;

public class DataHttpRequest {

private final String url2 = "http://www.google.ca";

private Logger logger = Logger.getLogger("DataRequest.java");

public void retrieveFile() {
    JsonpRequestBuilder builder = new JsonpRequestBuilder();

    builder.requestObject(url2,
            new AsyncCallback<ExcelFileJavascriptObject>() {

                @Override
                public void onFailure(Throwable caught) {
                    // TODO Auto-generated method stub
                    logger.severe(caught.getMessage());
                    logger.severe(caught.toString());
                }

                @Override
                public void onSuccess(ExcelFileJavascriptObject result) {
                    // TODO Auto-generated method stub
                    Window.alert(result.toString());
                }
            });
}

public void onModuleLoad() {
    retrieveFile();
}

}

package com.webapp.client;

import com.google.gwt.core.client.JavaScriptObject;

public class ExcelFileJavascriptObject extends JavaScriptObject{ protected ExcelFileJavascriptObject(){

}

}

Upvotes: 0

Views: 611

Answers (2)

Thomas Broyer
Thomas Broyer

Reputation: 64551

JSONP loads a JavaScript (using a <script> element to bypass SOP) with the convention that the script simply calls a function whose name is passed as argument in the script URL's query-string and which takes a single argument.

That means you cannot use it to load an Excel file.

In this post on the GWT forum, you're saying you want to “download an Excel file from a different domain onto the GWT servers and then parse it later on”.
If you want to load things from server to server, don't go through the client: use a URLConnection, or Apache HttpClient, JAX-RS 2 Client API (using Resteasy or Jersey as implementation), OkHttp, etc.

Upvotes: 0

You are breaking the 'Some Origin Policy' ajax restriction. You only could download that file using Ajax if your GWT app were deployed at 'http://www.google.ca' what I suppose it is not, isn't it?

The only way to get info from a different domain is: using 'JSONP' like you are trying to use, or 'CORS', in both cases the other domain should have an agreement with your application, in order to set the appropriate headers in case of you wanted to use CORS, or in order to wrap the response with a callback if you wanted JSONP, what it is not happening with the url you are trying to request.

If you want to explore both methods, this Ajax page I wrote for the gwtquery project could be useful.

Upvotes: 1

Related Questions