0

I have an Angular frontend which takes id and password for a login form,then I POST this value to a node.js server and from this server I send a JSON object back to Angular.

All works fine except the reading of this object from Angular. I think I've done the right steps, but the console shows me "undefined" as if it does not recognize the format.

I'm new with node and this is just an example to try to catch the response from node.


My server-side JS (Node.js) :

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.post('/login',function(req,res){
    console.log("sono nella post "+req.body.username);
    res.setHeader('Content-Type', 'application/json');
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Credentials", true);
    res.json({ack:'ok'});
});
app.listen(9080,function(){
    console.log('Server running at http://127.0.0.1:9080/');
});

My client-side JS (Angular) :

var app = angular.module('myApp',[]);
app.controller('myCtrl',function($scope,$http){
    $scope.check = function(data) {
        var id = data.form.id;
        var pwd = data.form.password;
        console.log("utente è "+id+",password è "+pwd);
        var msg = {username: id,password: pwd};
        $http({
            // without anything here, put * in app.post()
            url : 'http://localhost:9080/login',
            method : "POST",
            data : $.param(msg),
            responseType : "application/json",
            headers : {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(function(response) {
            console.log("Server response "+response.ack);
        });
    };
});

When running this, the console shows me Server response undefined.

Thanks in advance for the help.

3
  • 1
    First step should have been one of the following: add a debugger line to the callback and check the value of response, add a breakpoint and do the same, or console.log(response) directly. That likely would have solved the problem for you without you having to spend your time and our time here. Commented Feb 18, 2016 at 16:36
  • console.log will only get you so far. Open up chrome dev tools, put a breakpoint at the code you are having issues with and inspect the objects. Edit: if you are intent on using console.log then console.log(JSON.stringify(response)); will give you much richer feedback. Commented Feb 18, 2016 at 16:45
  • Thanks for the answer ,I've solved using response.data.ack. Thanks a lot! Commented Feb 18, 2016 at 21:02

2 Answers 2

3

According to the Angular $http documentation:

The response object has these properties:

  • data – {string|Object} – The response body transformed with the transform functions.
  • status – {number} – HTTP status code of the response.
  • headers – {function([headerName])} – Header getter function.
  • config – {Object} – The configuration object that was used to generate the request.
  • statusText – {string} – HTTP status text of the response.

So, if you try response.data.ack, then it might work.

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

Comments

1

try with

console.log("Server response "+ response.data);

instead of

console.log("Server response "+response.ack);

1 Comment

yeah! you get all the response in response.data then you can go further as per your object. Bdw! you can always use console.log(response.data) to check what you are actually getting in your console. Mark it as answer if you feel that it has solved your problem.

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.