user806672
user806672

Reputation:

GWT RequestBuilder POST JSON application/json

I'm trying to send JSON data to a server.

RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
builder.setHeader("Content-Type", "application/json");
builder.setRequestData(getJSONString());
builder.setCallback(myCallbackObject);
builder.send();

I do this in eclipse and I saw in the TCP/IP Monitor that my JSON Data is not transmitted as post in the request. If I ask for

builder.getRequestData();

I can see the JSON data is right there.

What do I need for the data to get on the server?

Upvotes: 3

Views: 8011

Answers (2)

Breno Souza
Breno Souza

Reputation: 1

To simply post JSON data with HTTP post, you can use GWT Overlay Types to define a post method using JQuery. A possible snapshot:

public static native void post(String json) /*-{
    var data = JSON.parse(json);
    $wnd.$.post(url, data);
}-*/;

I don't know if it is necessary to add JQuery to your HTML. I did, and worked. You can do this by adding the following line to your .html file:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Then, you can simply call that method in your java code:

post(json);

(:

Upvotes: 0

BobV
BobV

Reputation: 4173

You might be running into the browser's same-origin policy if the url that you are attempting to connect to is not from the same origin as your GWT nocache.js file. Is your callback's onFailure() being called? Also, see if Request.getSatusCode() returns 0, which is indicative of SOP problems.

Upvotes: 3

Related Questions