Send Json to nodejs server

3.6k views Asked by At

I develop an app with a REST nodejs server and a JavaScript / Zepto client.

I'm trying to send an json string from my client to my server

here is the client code:

$.ajax({
    type: 'POST',
    url: 'http://localhost:3000/request',
    data: JSON.stringify({test : "test"}),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    complete: callback,
    processData : false,
    success : function(){
        console.log("toto");
    },
    error : function(){
        console.log("erreur")
    }
});

And my node code :

app.post('/request', request.request);

// request.js
exports.request = function(req, res){
    console.log(req.body);
    res.header("Access-Control-Allow-Origin", "*");
    res.send("OK");
}

But my node console print this: {{test : "test"} : ""}

What's wrong?

1

There are 1 answers

0
TheHippo On BEST ANSWER

From reading the zepto documentation I would suggest you let zepto handling the encoding of the data.

Try:

$.ajax({
    type: 'POST',
    url: 'http://localhost:3000/request',
    data: {
        test: "test"
    },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    ....
});