GWT RequestBuilder - 跨站点请求 [英] GWT RequestBuilder - Cross Site Requests

查看:79
本文介绍了GWT RequestBuilder - 跨站点请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GWT Request构建器进行跨站点请求,但是我无法使其正常工作。正如你所看到的,这很大程度上是一个示例GWT项目,我已经通过 https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite 。但我仍然错过了一些东西。

I'm trying to make Cross Site Request using GWT Request builder, which i couldn't get it to work yet. As you can see, this is much of a Sample GWT Project and i have gone through https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite . But still i'm missing something.

我在这里发布代码。我缺少什么......?

I'm Posting the code here. What am i missing ..?

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发出跨站点请求。

Actually we can make Cross Site Requests from GWT RequestBuilder if we can set in Servlet Response Header

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

它很酷,如果有人需要GWT Project和Python Servlet,请让我知道,可以上传文件。

It's working Cool , if anyone need the GWT Project and Python Servlet, please do let me know, i can upload the files.

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

这篇关于GWT RequestBuilder - 跨站点请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆