Sudarshan
Sudarshan

Reputation: 31

POST not working with Node and Angular

I'm trying to make a POST request from Angular Factory to Node.

Angular Factory -

    function saveUser(userObject){
        var createUser = $http({
            method: 'POST',
            url: 'CreateUser',
            data: userObject,
            headers: {'Content-Type': 'application/json'}
        });

        return createUser.then(callSuccess, callError);
    }

Node-

function create(){
    app.post('/CreateUser', urlEncodedParser, function(request, response){
        var userData = {
            firstName : request.body.firstName,
            lastName : request.body.lastName,
            email : request.body.email,
            password : request.body.password,
            role : request.body.role
        };
        console.log(request);
        console.log(userData);

       dbOpperations.saveData(userData, 'UserTable');
    });
}

The call is made but I get response.body = {}

Upvotes: 0

Views: 129

Answers (2)

Sudarshan
Sudarshan

Reputation: 31

Thanks all for your help.

I'm able to POST the data and save on db.

I missed to use app.use(bodyParser.json());

Upvotes: 0

shan kulkarni
shan kulkarni

Reputation: 855

You are getting an empty response because you are not returning anything from node.

please use response.send()

response.send('success')

Upvotes: 1

Related Questions