For a webapp with a RESTful backend, I send to the json server using jquery $ post . Now, to my surprise, json is populated with a parameter key for the request form data, and not in the request body. I can come up with some other ways to do this , but the question is why it is not working as I expect.
On the server, I use scalatra and print some request information:
println("Request received:")
println(fromInputStream(request.getInputStream).getLines().mkString)
println("--------------")
println(request.getParameterMap.toString)
println("==============")
Now a simple twist that does what I think is right:
curl -X POST http://localhost:8080/x -H "Content-Type: application/json" -d '{"a":"b"}'
It produces:
Request received:
{"a":"b"}
-------------
{}
==============
And the html + js bit to illustrate the problem:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<body>
<button type="button" onclick="doPost()">
POST
</button>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function doPost() {
$.post("http://localhost:8080/x",
JSON.stringify({"a":"b"} ),
function(data) {});
}
</script>
</body>
</html>
It produces:
Request received:
--------------
{{"a":"b"}=[Ljava.lang.String;@7a2897ac}
==============
, $post json string , , . , , . , , , , $post.
UPDATE: jQuery contentType $.post