gpt4 book ai didi

javascript - GWT JSON 跨站请求失败

转载 作者:行者123 更新时间:2023-11-29 20:22:10 25 4
gpt4 key购买 nike

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”(它告诉我当处理程序的输入为空时)

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

最佳答案

我建议使用 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!
}
}
});

关于javascript - GWT JSON 跨站请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3431975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com