【问题标题】:GWT JSON cross-site request failsGWT JSON 跨站请求失败
【发布时间】:2010-08-07 20:44:51
【问题描述】:

imo 我按照这里的教程做了一切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);
     delete window[callback];
     delete window[callback + "done"];
   }, 1000);

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

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

顺便说一句,我说的是谷歌网站上的“3. 从远程服务器请求数据”

【问题讨论】:

    标签: javascript json gwt jsonp


    【解决方案1】:

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

     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!
             }
           }
         });
    

    【讨论】:

    • 我试过了。对于静态文件,它可以工作,但对于动态 json 文件,它总是失败。有什么想法吗?
    • 是否负责在服务器端生成 JSON 文件?那么也许这是一个错误:D(无意冒犯;))无论哪种方式,尝试通过浏览器或 wget 下载动态和静态文件并比较和验证它们:jsonlint.com 是一个很棒的 JSON 验证器。另外:“它总是失败”-您能否更具体地说明它是如何“失败”的? :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2015-01-28
    • 1970-01-01
    • 2023-03-13
    • 2013-03-26
    • 1970-01-01
    相关资源
    最近更新 更多