我有一个GWT应用程序的输入(让我们说姓名,地址,电子邮件)。在用户输入所有必需的字段并按下提交按钮后,PHP页面将显示GWT应用程序的输入。我如何将GWT应用程序连接到PHP ?我现在使用请求生成器。我还必须使用XML将GWT输入传递给PHP吗?请帮助。我刚开始学习GWT。
你实际上不需要RequestBuilder来做这样的事情。
如果重定向到PHP url并将输入附加为GET参数就足够了。比如在点击处理程序中你可以这样做:
submitButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String linkURL = "somePage.php?name="+name+"&address="+address+"&email="+email;
Window.Location.assign(linkURL);
}
});
然后在PHP页面中,您可以通过以下方式检索参数:
$name = $_GET['name'];
$address = $_GET['address'];
$email = $_GET['email'];
如果你想使用RequetBuilder,你必须这样做:
submitButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
RequestBuilder request = new RequestBuilder(POST,PHP_URL);
JSONObject jsonValue = new JSONObject();
jsonValue.put("name", new JSONString(name));
jsonValue.put("address", new JSONString(address));
jsonValue.put("email", new JSONString(email));
request.setHeader("Content-Type", "application/json");
request.sendRequest(jsonValue.toString(),new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
//retrieve a uniqueid or so and redirect to the PHP page which displays the infos
} else {
// displayError("Couldn't retrieve
}
}
@Override
public void onError(Request request, Throwable exception) {
//displayError("Couldn't retrieve JSON");
}
});
}
});
在服务器上,您只需访问全局$_POST变量来获取值:
$name = @_POST['name']