0

I am making an Ajax Post request:

$.ajax({
        type:"POST",
        dataType:"json",
        contentType: "application/json",
        data:newWorkLog,
        url:"/add",
      })
        .done(function(response){
          console.log("Response of update: ",response)
        })
        .fail(function(xhr, textStatus, errorThrown){
          console.log("ERROR: ",xhr.responseText)
          return xhr.responseText;
        });

and was expecting to pass that newWorkLog object to an API method through my node.js server:

var bodyParser = require('body-parser');
app.use(bodyParser.json())

app.post('/add', function(req, res){
        console.log(req.body) //This doesnt output anything
        res.send(JSON.stringify(req.body));
    });

After trying some approaches, I decided to only check what is being sent to my server.

Doing this, the message I get is:

SyntaxError: Unexpected token # in JSON at position 0
    at JSON.parse (<anonymous>)
    at createStrictSyntaxError (C:\working\app\node_modules\body-parser\lib\types\json.js:157:10)
    at parse (C:\working\app\node_modules\body-parser\lib\types\json.js:83:15)
    at C:\working\app\node_modules\body-parser\lib\read.js:121:18
    at invokeCallback (C:\working\app\node_modules\raw-body\index.js:224:16)
    at done (C:\working\app\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (C:\working\app\node_modules\raw-body\index.js:273:7)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1055:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

When I console.log my newWorkLog object, I can see from my client side, the correct json object.

when I check the param on the console I see the request payload and it looks like:

user%5Bid%5D=109&user%5BuserName%5D=myname

What could be causing that error?

7
  • 1
    In order to parse body you need to use bodyParser and you can it like this app.use(express.bodyParser()); Commented Mar 21, 2018 at 11:49
  • I am using app.use(bodyParser.json()) I edited the question Commented Mar 21, 2018 at 11:51
  • var express = require('express') , app = express.createServer(); app.use(express.bodyParser()); app.post('/', function(request, response){ console.log(request.body); // your JSON response.send(request.body); // echo the result back }); app.listen(3000); try to redefine the server Commented Mar 21, 2018 at 12:01
  • I got that error: express.createServer is not a function Commented Mar 21, 2018 at 12:04
  • npm install express --save Commented Mar 21, 2018 at 12:05

1 Answer 1

1

You say newWorkLog is an object so you need to convert it to json to send it in your request.

$.ajax({
    type:"POST",
    dataType:"json",
    contentType: "application/json",
    data:JSON.stringify(newWorkLog),
    url:"/add",
})
.done(function(response){
      console.log("Response of update: ",response)
})
.fail(function(xhr, textStatus, errorThrown){
      console.log("ERROR: ",xhr.responseText)
      return xhr.responseText;
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.