0

I have to sent a post request to my node server running localhost:3001. I successfully completed the request and getting the post data in node server but the data is poorly formatted.

AngularJS:

function MyCtrl1($scope,$http,$location) {     
    $scope.user = { };
    $scope.login = function() { 

    $http.post("http://localhost:3001/login", 
                $scope.user,
                {'Content-Type':  'application/json'}).success(function(result) {
                    $scope.resultPost = result;
                    $location.path('/');

                }).error(function() {
                    console.log("error");
                });
    };
}  

Nodejs:

app.post('/login', function(req,res) {
    console.log(JSON.stringify(req.body));

    res.end('ok');
});

log : {"{\"username\":\"test\",\"password\":\"pass123\"}":""}

is there any way to get a formatted data here?

1
  • I think your want JSON.parse(str); Commented Aug 28, 2013 at 1:42

1 Answer 1

1

You can use JSON.parse() to parse the body to JavaScript object.

var obj = JSON.parse(req.body);
var username = obj.username;
var password = obj.password;
Sign up to request clarification or add additional context in comments.

3 Comments

Yes its working ,now i getting { '{"username":"test","password":"pass123"}': '' } but how i get the value of username and password from it..
@Nish If it is successfully converted to object, then you can just access the field using dot notion. Updated the answer.
when i trying to convert it the node server throws a error SyntaxError: Unexpected token o at Object.parse (native)

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.