user3813234
user3813234

Reputation: 1682

gwt: send php post request

I am trying to communicate to a php server from my gwt project.

I already got a GET request to work, however, my POST request doesn't work so far.

Here's my code:

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

I can see that the post request goes out and the request payload is a json-object. However, when I try to access the data in the php script, I get the error that the index name is undefined.

Here's the PHP:

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

Is there something wrong with my PHP?

Does anyone have a working example for this?

Upvotes: 1

Views: 232

Answers (1)

thst
thst

Reputation: 4602

While I haven't checked the PHP documentation so far, I tend to remember, that $POST contains the post request's variables, especially useful in a x-www-form-urlencoded request. .. Checked it, yes. I am right :-)

What you actually want is to read the body of the post request and parse it's JSON content to a PHP array or hash.

To read the body see here: How to get body of a POST in php?

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

Parsing json is described here: Parsing JSON file with PHP

I will not quote the code from there, as it maybe does not exactly fit your needs, but you look for json_decode($json, TRUE).

Upvotes: 1

Related Questions