GWT JSON跨站请求失败

发布于 2024-09-13 04:17:54 字数 1844 浏览 3 评论 0原文

我认为我按照这里的教程进行了所有操作 Google 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]
     [email protected]::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"]) {
       [email protected]::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(null);
     }

     // [5] Cleanup. Remove script and callback elements.
     document.body.removeChild(script);
     delete window[callback];
     delete window[callback + "done"];
   }, 1000);

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

但它一直让我失败..所有其他方法也都写了..我只是可以理解为什么它每次都会告诉我“无法检索JSON”(它告诉我当处理程序的输入为空时)

顺便说一句,我正在谈论google网站上的“3.从远程服务器请求数据”

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]
     [email protected]::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"]) {
       [email protected]::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(null);
     }

     // [5] Cleanup. Remove script and callback elements.
     document.body.removeChild(script);
     delete 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-09-20 04:17:54

我建议使用 JsonpRequestBuilder,而不是所有这些 JSNI 代码 - 没有 JSNI 代码(可能只是一些覆盖类型),更容易调试等。

 String url = "http://www.google.com/calendar/feeds/[email protected]/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!
         }
       }
     });

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/[email protected]/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!
         }
       }
     });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文