AngularJS log JSON

Is there any way to output the JSON response that comes from the server? I am trying to do the following: with an undesired result:

$http({ method: 'GET', url: 'getUser' }) .success(function (data,status) { $scope.user = data; console.log("User="+data);}) .error(function (data,status) { $scope.status = status; if(status==403) $location.path( "/" ); }); 

Output:

 User=[object Object] 
+7
json angularjs logging
source share
4 answers

You can stringify before printing.

 console.log("User = " + JSON.stringify(data)); 
+13
source share

You can also use angular.toJson() if you do not want to add a new dependency:

 $log.debug(angular.toJson(data, true)); 

Link

+7
source share

Depending on your browser, the log function may take several parameters. For example, Chrome supports the following:

 console.log("User = ", data); 

When you do this, data will appear in the object browser, which you can show / collapse the hierarchy. This is extremely helpful. Since I am developing in Chrome, I really prefer this method (more JSON.stringify ), because it prints less and debugging works better.

If you are stuck with debugging in a browser that does not support this syntax, then JSON.stringify is probably the best choice.

+4
source share

console.dir(object) is what you are looking for. It is currently widely supported in all browsers (IE9 +). The output can be viewed here .

0
source share

All Articles