Jquery ajax get node express how to get json data?

I am trying to send JSON data to a node instance using an ajax get request clientside request as shown below:

var parameters = { username: 'test' }; $.ajax({ url: url, data: JSON.stringify(parameters), success: function {}, dataType: 'json' }); 

Using firebug, I see that it sends encoded http: // server / web_svc? Username = test

in my express node method:

 function svc_method(req, res) { var username = req.body.username; } 

req.body.username undefined. It only works if I send instead.

How to fix this problem? I have the line app.use (express.bodyParser ()) at the top of app.configure ().

+4
source share
1 answer

You should use req.query.username , since you want to get the query string parameter, see the official Express manual: http://expressjs.com/api.html#req.query

+12
source

Source: https://habr.com/ru/post/1386165/


All Articles