【问题标题】:GWT RequestBuilder - Cross Site RequestsGWT RequestBuilder - 跨站点请求
【发布时间】:2012-06-20 14:04:07
【问题描述】:

我正在尝试使用 GWT 请求构建器进行跨站点请求,但我还不能让它工作。如您所见,这是一个示例 GWT 项目,我已经完成了 https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite 。但我仍然缺少一些东西。

我在这里发布代码。我错过了什么..?

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

【问题讨论】:

    标签: java gwt httprequest


    【解决方案1】:

    如果我们可以在 Servlet Response Header 中设置,实际上我们可以从 GWT RequestBuilder 发出跨站请求

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

    很酷,如果有人需要 GWT 项目和 Python Servlet,请告诉我,我可以上传文件。

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

    【讨论】:

    • 我一直在寻找这样的解决方案,我不想在 GWT Java 中使用本机 JS。 [对不起乔纳斯,我真的很喜欢这个标题的想法]
    【解决方案2】:

    您错过了阅读教程。

    直接引用tutorial

    RequestBuilder 代码被对 getJson 方法的调用所取代。所以 您不再需要在 refreshWatchList 方法中添加以下代码

    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url); 尝试 { 请求请求 = builder.sendRequest(null, new RequestCallback() { public void onError(Request request, Throwable exception) { displayError("无法获取 JSON"); } 公共无效onResponseReceived(请求请求,响应响应){ if (200 == response.getStatusCode()) { updateTable(asArrayOfStockData(response.getText())); } 别的 { displayError("无法检索 JSON (" + response.getStatusText() + ")"); } } }); } 捕捉(请求异常 e){ displayError("无法获取 JSON"); }

    这大致是你所拥有的,应该用下面几行教程中给出的 JSNI 函数代替:

    /** * 调用远程服务器。 */ public native static void getJson(int requestId, String url, StockWatcher 处理程序)/*-{ var回调=“回调”+ requestId; // [1] 创建一个脚本元素。 var script = document.createElement("script"); script.setAttribute("src", url+callback); script.setAttribute("type", "text/javascript"); // [2] 在窗口对象上定义回调函数。 窗口[回调] = 函数(jsonObj){ // [3] handler.@com.google.gwt.sample.stockwatcher.client.StockWatcher::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj); 窗口[回调+“完成”] = true; } ...

    【讨论】:

    • 我尝试了同样的事情,但它适用于某些链接,而不适用于其他一些链接你知道吗?为什么有些网址没有回复我。 GWT 获得成功响应,但在萤火虫中响应为空白。可能是服务器问题?。
    猜你喜欢
    • 1970-01-01
    • 2013-03-26
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    相关资源
    最近更新 更多