开发者

GWT JSON cross-site request fails

开发者 https://www.devze.com 2023-01-11 02:38 出处:网络
imo i did everything according to the tutorial here Googles x-site /** * Make call to remote server. */ public native static void getJson(int requestId, String url,

imo i did everything according to the tutorial here Googles x-site

    /**
   * 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;
   }

   // [4] JSON download has 1-second timeout.
   setTimeout(function() {
     if (!window[callback + "done"]) {
       handler.@com.google.gwt.sample.stockwatcher.client.StockWatcher::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(null);
     }

     // [5] Cleanup. Remove script and callback elements.
     document.body.removeChild(script);
     del开发者_高级运维ete window[callback];
     delete window[callback + "done"];
   }, 1000);

   // [6] Attach the script element to the document body.
   document.body.appendChild(script);
  }-*/;

but it keeps failing me.. all other methos are also written.. i just can understand why it tells me every time that "Couldn't retrieve JSON" (it tells that when handler's input is null)

btw i am talking about the "3. Requesting the data from the remote server" on the googles site


I'd suggest using JsonpRequestBuilder, instead of all this JSNI code - no JSNI code (maybe just some overlay types), easier to debug, etc.

 String url = "http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full" +
     "?alt=json-in-script";
 JsonpRequestBuilder jsonp = new JsonpRequestBuilder(); // No JSNI!
 jsonp.requestObject(url,
     new AsyncCallback<Feed>() { // Type-safe!
       public void onFailure(Throwable throwable) {
         // Easy to debug! (hopefully)
       }

       public void onSuccess(Feed feed) {
         // Success!
         }
       }
     });
0

精彩评论

暂无评论...
验证码 换一张
取 消