Hey all I am trying to POST some json data to my android app via Jquery AJAX and using NanoHTTPd as the web server.
I can seem to call the correct url doing this:
var reqData = JSON.stringify({"AppName": "test", "Enabled": "yes" });
$.ajax('http://10.0.2.16:8765/data', {
data: reqData,
dataType: 'json',
contentType: "application/json",
async: false,
type: 'POST',
success: function (data, textStatus, jqXHR) {
alert(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error:" + errorThrown + ' ' + reqData);
}
});
Once I fire off that call above it goes to this java/nanoHTTPd code:
if (uri.equals("/data")) {
try {
session.parseBody(new HashMap<String, String>());
System.out.println( session.getMethod() + " " + session.getParms() );
String postBody = session.getQueryParameterString();
String postParameter = session.getParms().get("AppName");
return newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
MIME_PLAINTEXT,
"It was good!"
);
} catch (IOException e) {
e.printStackTrace();
return newFixedLengthResponse(Response.Status.NOT_FOUND,"","ERROR");
} catch (ResponseException e) {
e.printStackTrace();
return newFixedLengthResponse(Response.Status.NOT_FOUND,"","ERROR");
}
}
But I am getting null for the postBody? The System.out.println
shows I/System.out: POST {}
The error alert for the ajax error part dispays like this:
What would I be missing here in order to get the post data ajax is sending?