我創建了一個非常簡單的使用HTTP Post方法的servlet。我使用一個簡單的HTML表單在我的本地Apache Tomcat服務器上進行了測試。我想將它與我的GWT應用程序集成。我可以使用FormPanel調用它 - 在這種情況下,它會下載內容,並且在瀏覽器窗口中閃爍。GWT RequestBuilder發佈回覆返回0 StatusCode
我知道我需要使用RequestBuilder來訪問它。但我response.getStatusCode()在我的重載公共無效onResponseReceived(請求請求,響應response)方法總是返回狀態爲0和response.getText()返回null
String url = "http://localhost:8080/servlets/servlet/ShapeColor";
builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
try {
String json = getJSONString();
//builder.setTimeoutMillis(10000);
builder.setHeader("Access-Control-Allow-Origin", "*");
builder.setHeader("Content-type", "application/x-www-form-urlencoded");
builder.sendRequest(json, new RequestCallback() {
@Override
public void onError(Request request, Throwable exception) {
Window.alert("Couldn't retrieve JSON");
}
@Override
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
System.out.println("res:"+response.getText());
} else {
System.out.println("err: " + response.getStatusCode()+","+response.getText());
}
}
});
//Request response = builder.send();
} catch (RequestException e) {
// TODO Auto-generated catch block
}
我已經嘗試了許多事情,包括改變我的servlet以下CORS參考(https://code.google.com/p/gwtquery/wiki/Ajax#CORS_%28Cross_Origin_Resource_Sharing%29)
它始終在瀏覽器上使用我的test.html,但不是從我的應用程序。雖然,onResponseReceived方法總是被調用
感謝
KKM