GWT RequestBuilder - cross-site requests

I am trying to make a Cross Site Request using the GWT Request builder, which I could not get it to work. As you can see, this is a big part of the GWT trial project, and I went through https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite . But still, I missed something.

I am posting the code here. What am I missing ..?

package com.gwt.reqbuilder.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.http.client.Request; import com.google.gwt.http.client.RequestBuilder; import com.google.gwt.http.client.RequestCallback; import com.google.gwt.http.client.RequestException; import com.google.gwt.http.client.Response; import com.google.gwt.http.client.URL; import com.google.gwt.user.client.Window; public class GWTRequestBuilder implements EntryPoint { private static final String JSON_URL = "http://localhost:8000/?q=ABC&callback=callback125"; public void onModuleLoad() { GWTPOSTHTTP(); } public void GWTPOSTHTTP() { String postUrl="http://localhost:8000"; String requestData="q=ABC&callback=callback125"; RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, postUrl); try { builder.sendRequest(requestData.toString(), new RequestCallback() { public void onError(Request request, Throwable e) { Window.alert(e.getMessage()); } public void onResponseReceived(Request request, Response response) { if (200 == response.getStatusCode()) { Window.alert(response.getText()); } else { Window.alert("Received HTTP status code other than 200 : "+ response.getStatusText()); } } }); } catch (RequestException e) { // Couldn't connect to server Window.alert(e.getMessage()); } } } 
+6
source share
2 answers

In fact, we can make cross-site requests from the GWT RequestBuilder if we can set the servlet response header

 Response.setHeader("Access-Control-Allow-Origin","http://myhttpserver"); 

It works great, if someone needs a GWT project and a Python servlet, please let me know, I can upload files.

 GWT Client Code : https://github.com/manikandaraj/MLabs/tree/master/GWT/GWTClient 
+5
source

You skipped to finish reading the tutorial.

Direct quote from tutorial :

RequestBuilder code is replaced by a call to the getJson method. So, you no longer need the following code in the refreshWatchList method:

  RequestBuilder builder = new RequestBuilder (RequestBuilder.GET, url);

 try {
   Request request = builder.sendRequest (null, new RequestCallback () {
     public void onError (Request request, Throwable exception) {
       displayError ("Couldn't retrieve JSON");
     }

     public void onResponseReceived (Request request, Response response) {
       if (200 == response.getStatusCode ()) {
         updateTable (asArrayOfStockData (response.getText ()));
       } else {
           displayError ("Couldn't retrieve JSON (" + response.getStatusText ()
             + ")");
       }
     }
   });
 } catch (RequestException e) {
   displayError ("Couldn't retrieve JSON");
 }

In general, what you have, and it should be replaced by the JSNI function specified in the tutorial, is a few lines below:

  / **
    * Make call to remote server.
    * /
   public native static void getJson (int requestId, String url,
       StockWatcher handler) / * - {
    var callback = "callback" + requestId;

    // [1] Create a script element.
    var script = document.createElement ("script");
    script.setAttribute ("src", url + callback);
    script.setAttribute ("type", "text / javascript");

    // [2] Define the callback function on the window object.
    window [callback] = function (jsonObj) {
    // [3]
      handler.@com.google.gwt.sample.stockwatcher.client.StockWatcher :: handleJsonResponse (Lcom / google / gwt / core / client / JavaScriptObject;) (jsonObj);
      window [callback + "done"] = true;
    }

     ...
+3
source

Source: https://habr.com/ru/post/918566/


All Articles