Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

java - GWT RequestBuilder - Cross Site Requests

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());
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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");

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...