5

I'm trying to send the node instance JSON data using an ajax get request clientside as shown below:

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

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

in my node express method:

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

req.body.username is undefined. It only works if I post instead of get.

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

1 Answer 1

13

You should use req.query.username since you want to get a query string param, check the official Express guide: http://expressjs.com/api.html#req.query

Sign up to request clarification or add additional context in comments.

2 Comments

Query is an object, so you can use req.query.username which is the preferred syntax in javascript.
Thanks for your valid observation, indeed req.query.username is the preferred syntax. I made a quick copy-paste from some code :) Edited now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.