Mirko
Mirko

Reputation: 137

Cannot reading json response in angular

js and node.js and I'm having some troubles with json response.I have a node.js server that performs a get to a REST api,and the json result must be sent to an angular client but when I try to read that json file,I obtain [Object,Object].I can't figure out what I am missing.Thanks for helping me. Json file response from the REST API:

[ { cityPK: { name: 'Lodi', region: 'Lombardia' } },
{ cityPK: { name: 'Frosinone', region: 'Lazio' } },
{ cityPK: { name: 'Roma', region: 'Lazio' } },
{ cityPK: { name: 'Tivoli', region: 'Lazio' } } ]

node.js file:

var http = require('http');
var request = require('request');
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);
    var uid = req.body.username;
    var upwd = req.body.password;
    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);



    request({
        url: "http://localhost:8080/RestServiceAdmin/webresources/entities.city",
        method: "GET",
        json: true ,
        headers:[{'content-type': 'application/json'}] 
    }, function (error, response, body){
           if(!error & response.statusCode === 200){
               res.json(body);
           }
    });
});
    app.listen(9080,function(){
    console.log('Server running at http://127.0.0.1:9080/');
});

Angular client:

 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({
                    url:'http://localhost:9080/login', //senza niente qui,metti * in app.post()
                    method:"POST",
                    data: $.param(msg),
                    responseType: "application/json",
                    headers: {'Content-Type': 'application/x-www-form-urlencoded',
                              'Accept': 'application/json' 
                              }})
                        .then(function(response){

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

I think that "response.data" should give me the json file,while it returns me [Object,Object].What's wrong with me?

Upvotes: 0

Views: 1034

Answers (1)

MrHaze
MrHaze

Reputation: 3996

Solution

You're almost there!

Just need to change the + in your log statement to a ,.

like this:

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

Reason

The + operator will try to concatenate the object to a string, the object isn't a string so it calls the toString() function, which shows you [Object,Object], meaning that you have an object which contains an object.

When you use a comma it will inspect the object.

From the toString() documentation:

Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString() method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString() returns "[object type]", where type is the object type. The following code illustrates this:

Upvotes: 1

Related Questions