gwt: 发送 php post 请求

gwt: send php post request

我正在尝试从我的 gwt 项目与 php 服务器通信。

我已经收到一个 GET 请求,但是,我的 POST 请求目前还没有成功。

这是我的代码:

Button synchronize = new Button("synchronize ",
                new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        String myurl = URL
                                .encode("php/test.php");

                        RequestBuilder builder = new RequestBuilder(
                                RequestBuilder.POST, myurl);

                        JSONObject jsonValue = new JSONObject();
                        jsonValue.put("name", new JSONString("Abc"));
                        builder.setHeader("Content-Type", "application/json");

                        try {
                            Request request = builder.sendRequest(jsonValue.toString(),
                                    new RequestCallback() {
                                        public void onError(Request request,
                                                Throwable exception) {
                                            processResponse("ERROR");
                                        }

                                        public void onResponseReceived(
                                                Request request,
                                                Response response) {
                                            if (200 == response.getStatusCode()) {

                                                processResponse(response
                                                        .getText());
                                            } else {
                                                processResponse("ERROR");
                                            }
                                        }
                                    });

                        } catch (RequestException e) {
                            processResponse("ERROR");
                        }

                    }
                });

public void processResponse(String responseString) {
    Window.alert(responseString);
}

我可以看到 post 请求发出并且请求负载是一个 json 对象。但是,当我尝试访问 php 脚本中的数据时,出现索引 name 未定义的错误。

这是 PHP:

<?php
echo $_POST["name"];
?>

我的PHP有问题吗?

有人有这方面的工作示例吗?

虽然到目前为止我还没有检查 PHP 文档,但我倾向于记住,$POST 包含 post 请求的变量,在 x-www-form-urlencoded 要求。 .. 检查了一下,是的。我是对的:-)

您真正想要的是读取 post 请求的正文并将其 JSON 内容解析为 PHP 数组或散列。

要阅读正文请看这里:How to get body of a POST in php?

$entityBody = file_get_contents('php://input');

解析 json 描述如下:Parsing JSON file with PHP

我不会引用那里的代码,因为它可能不完全符合您的需求,但您可以寻找 json_decode($json, TRUE).