Currently I'm trying to process some JSON type data post from AJAX request with nodejs http server.
The problem is, it looks like request model is converting the complex JSON obj to array format. which results to server JSON.parse
error as the received string is not a correct JSON string.
The client AJAX code:
$.ajax({ "type": "POST", "url": "localhost:8080/testjson", "async": true, "crossDomain": true, dataType: "json", contentType: "application/json;charset=utf-8", "data": { "data":{ "bar":"foo" }, "desc":"hello", "user":{ "info":"test", "name":"admin" } }, success: function(json) { console.log(json); }, error: function(XMLHttpRequest, textStatus, errorThrown) { console.error(textStatus); } });
nodejs server code:
req.on('data', function(chunk) { console.log(JSON.parse(chunk)); })
But it looks like the server received string is something like:
data[bar]=foo&description=hello&user[info]=test&user[name]=admin
Definitely not a valid JSON object or JSON string.
Any idea on how to process JSON data post with nodejs? Don't want to stringify the JSON data on client side as it is a restful, so better to change the nodejs server to process original JSON object directly.