0

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.

zjx
  • 56
  • 5
  • You say you don't want to stringify the data on the client side, but that's how you send JSON. It has to be stringified into JSON. That's JSON is. See the question yours has been marked a dup of for how to send JSON. – jfriend00 Aug 14 '17 at 04:16
  • Also, btw if you're using Express in node.js, you can use the `body-parser` middleware to automatically read and parse your JSON from the POST and put the result into `req.body` where you can access the already parsed object directly. – jfriend00 Aug 14 '17 at 04:18
  • @jfriend00, actually i am trying to send a json object not a json string from client side. if i put JSON.stringify(jsonObject) as the ajax data parameter then in server side I can parse it to json object back. but if I put a json object directly here then seems it is converted to array format.. – zjx Aug 14 '17 at 04:35
  • @jfriend00 currently i am not using express or similar framework. just want to test some of the basic functions of nodejs without framework.. quite new to nodejs.. – zjx Aug 14 '17 at 04:38
  • First off, there is no such thing as a JSON object. JSON is a serialized text format - perhaps you meant "Javascript object". Second off, you can't send a Javascript object - that's something that is internal to the JS interpreter and can't be directly sent over the wire. If you want to send it, you have to serialize it some way and the JSON text format is the standard way to serialize it. You call `JSON.stringify(obj)` to serialize it into JSON. If you read the accepted answer on the question yours has been marked a duplicate of, it shows how to do it. – jfriend00 Aug 14 '17 at 04:38
  • A plain http server is good for learning, but you will very quickly find that using Express and many of the NPM modules built for it accelerates your development a lot as you get to use already tested code that solves many of your problems. The only hard part is becoming familiar with what is out there and learning how to use it. – jfriend00 Aug 14 '17 at 04:42
  • @jfriend00, thanks a lot. i think i got your point. any thing send via http should be convert to pain text firstly no matter what background it is.. is that correct? i also noticed some other restful ws which accept json request is also using text.. like es do: curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "from": 10, "size": 99 }' – zjx Aug 14 '17 at 04:47
  • It doesn't HAVE to be text as binary can be sent over http too in some circumstances, but the point is that a Javascript object has to be converted into something in order to send it and the standard way of doing so these days is to convert it to JSON which is a serialized text format. Since your server-side code was using `JSON.parse()`, your server code was already ready for it to be JSON. – jfriend00 Aug 14 '17 at 04:53

0 Answers0