java - GWT RequestBuilder - 跨站请求

标签 java gwt httprequest

我正在尝试使用 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());
        }
    }
}

最佳答案

如果我们可以在 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

关于java - GWT RequestBuilder - 跨站请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11121374/

相关文章:

java - 在 GWT 中实现拖放列表的最佳方式

java - 如何从非 GWT(但 Java)gapplication 在服务器上调用 GWT RPC 方法?

android - 将对象数组作为请求中的 url 参数传递

c# - 匿名事件

java - Spring 中的内容类型问题

java - 如何将样式应用于特定的单元格表格行?

java - 是否可以使用@RequestMapping检查 header 的值

java - 从AudioInputStream重写为ByteArrayOutputStream的优化

Java NIO FileChannel - 从 Android TUN 网络接口(interface)读取空值

c# - 使用 C# 的 HTTP 请求