2
"name": "body-parser", "version": "1.13.3",

my json request body is{user:'guow'} , but express request.body is { '{user:\'guow\'}': '' }

This is configuration of my express app

var bodyParser = require('body-parser'); //
var multer = require('multer');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: false })); // for parsing application/x-www-form-urlencoded //
app.use(multer());

I am sending request with jquery and my code is

$.ajax({ type: “POST”, url: “/login”, cache: false, dataType: 'json', data:“{user:'guow'}"});

Will someone encountered such a problem?

Vishnu
  • 11,614
  • 6
  • 51
  • 90
user891530
  • 23
  • 3
  • How do you post your request? – Vsevolod Goloviznin Aug 26 '15 at 09:24
  • jQuery$.ajax({ type: “POST”, url: “/login”, cache: false, dataType: 'json', data:“{user:'guow'}"}); – user891530 Aug 26 '15 at 09:29
  • how did you configure your body-parser to your express app. Add the configuration code. – Vishnu Aug 26 '15 at 09:31
  • var bodyParser = require('body-parser'); //var multer = require('multer'); app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: false })); // for parsing application/x-www-form-urlencoded //app.use(multer()); // for parsing multipart/form-data – user891530 Aug 26 '15 at 09:31
  • 1
    @user891530 you don't need to send a stringified object. Just use: `$.ajax({ type: “POST”, url: “/login”, cache: false, dataType: 'json', data: {user: "grow"});` – Vsevolod Goloviznin Aug 26 '15 at 09:37
  • @VsevolodGoloviznin Thank you, this is right, but why? – user891530 Aug 26 '15 at 09:44
  • @user891530 that's because jQuery will automatically serialize an object to string for you. In your code it serialized a string which doesn't give any effect – Vsevolod Goloviznin Aug 26 '15 at 09:58

3 Answers3

3

I am no expert in node.js, but I had similar problem in other context.

I believe you should change ' (single quote) in " (double quote) in your json request, as single quotes will be interpreted as literals. json wants that request to be stated like {"user":"guow"}.

just my two cents.

Also, the accepted answer from this other question matters:

jQuery single quote in JSON response

Community
  • 1
  • 1
Leo128
  • 163
  • 12
  • The result is the same. – user891530 Aug 26 '15 at 09:35
  • as I stated, I am no expert in node.js. Actually I would have posted a comment but I cannot yet because of low rep. it also looks like your request body became a key in express request.body. I agree you should check the way you post the request. – Leo128 Aug 26 '15 at 09:40
  • may I ask the exact reason for downvote? just for curiosity. lowering my rep does not help me ever be able to post comments instead of answers when a comment might have helped. no pun intended – Leo128 Aug 26 '15 at 09:50
  • thank mate, now my life on this community will improve (and so will my answers!) – Leo128 Aug 26 '15 at 09:55
2

Somewhere in your code the whole body {user:'guow'} is set as a string key for the object. Note the single quotes around the original object { '{user:\'guow\'}': '' }

You need to check how your object is passed to the send function. Something like

res.send({user:'guow'});

Should be fine.

lrnop
  • 66
  • 1
-1
$.ajax({ type: "POST",
         url: "/login",
         cache: false,
         dataType: 'json',
         data:{"user":"guow"}
      });
Vishnu
  • 11,614
  • 6
  • 51
  • 90
  • If you want to send JSON, then you have to send a string. The previous value was just invalid JSON. If you pass jQuery an object then it will send www-form-urlencoded encoded data, not JSON. – Quentin Aug 26 '15 at 09:50
  • @Quentin: you mean data:JSON.stringify({"user":"guow"}) would have been the correct way? – Leo128 Aug 26 '15 at 09:57
  • That or manually constructing a string that is valid JSON. You'd also need to specify the content-type correctly since jQuery's default is wrong for JSON. – Quentin Aug 26 '15 at 10:00
  • Re latest edit: Setting the content type to JSON while setting the value of `data` to an Object isn't going to work. You have to assign a JSON encoded string to `data`. – Quentin Aug 26 '15 at 10:05
  • @Quentin If you know the proper correct answer, Why don't you add or edit the answer. – Vishnu Aug 26 '15 at 10:07
  • @Mahesh — I'm not familiar enough with Express to give a confident answer (and even less so without the information that was only very recently edited into the question). I am familiar enough with jQuery to spot when an answer is wrong. – Quentin Aug 26 '15 at 10:11