AngularJS: read response data from server

angularjs http get example with parameters
angularjs get json data from url
angularjs rest api call example
ajax in angular 8
angularjs http interceptor
angularjs $http post json example
how to get data from api in angularjs
how to retrieve json data in angularjs

I have a problem which should be wasy to solve, but I just cant figure out what I am doing wrong. I receive data through an $http request.

alert(data)

gives me object object

alert(data.response)

gives me {"id":"123456","post_id":"12345"}

alert (data.response.id)

gives me undefined

My question: I want to get the ID. Why does the last expression give me undefined and not the ID? Do I have to transform the data in some way?

I am thankful for any hints!


It looks like your data.response is a string. You use angular.fromJson to convert it to object ie :

$scope.temp = angular.fromJson($scope.data.response);

please see working demo below

var app = angular.module('app', []);

app.controller('firstCtrl', function($scope){
 $scope.data = {
 response:'{"id":"123456","post_id":"12345"}'
 };
  
  alert($scope.data);
  alert($scope.data.response);
  
   alert($scope.data.response.id);
  
  $scope.temp = angular.fromJson($scope.data.response);
  
  alert($scope.temp.id);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
  <div ng-controller="firstCtrl">

      </div>
</body>

AngularJS AJAX, All shortcut methods require passing in the URL, and request data must be When caching is enabled, $http stores the response from the server using the  Find angularjs consulting on KensaQ.com. KensaQ updates its results daily to help you find what you are looking for. Search Now!


var app = angular.module('myApp', []);

app.controller('list_employeesController', function($scope, $http) {
  // email = $scope.email;
  // psw = $scope.psw;
  $http({
    method: "GET",
    url: "http://localhost:3000/employees",
    params: {}

  }).then(function mySuccess(response) {
      // a string, or an object, carrying the response from the server.
      $scope.myRes = response.data;
      $scope.statuscode = response.status;

    }, function myError(response) {
      $scope.myRes = response.statusText;
  });
});

Simply Here myRes has the response data to this request.

And we can display it in HTML file using expression syntax in angular

 <h2> Result : {{myRes}} </h2>

get(url, [config]), Learn how to get server side data using AngularJS $http.get method. This sends partial ajax request and receive response from the server. Content updated daily for angularjs development. Get reliable information in seconds. Search Teoma today!


var app = angular.module('myApp', []);

app.controller('student_Controller', function($scope, $http) {

  $http({
    method: "GET",
    url: "http://localhost:8000/employees",
  }).then(function mySuccess(response) {

      $scope.res= response.data;
      $scope.statuscode = response.status;

    }, function myError(response) {
      console.log("response--"+response);
  });
});

How to get data from Server in AngularJS?, The $http service is used to send or receive data from the remote server using which includes various methods to process the response of http GET request. It means that data.response is a string, not an Object. Otherwise similarly to alert (data) you would have got [object Object]. – dfsq Nov 24 '14 at 22:03. 2. Your response is probably a string. You'll need to turn it into JSON first. Call the function angular.fromJson on $scope.data.response then you can call the id.


$http Service in AngularJS, The $http service is how AngularJS makes rest API calls. It's capable of making GET-ing Data. Let's use the get() method to retrieve some data from a server:. JSONP or “JSON with padding” is the communication technique which allows for data to be requested from a server under a different domain (also known as a Cross Origin Request). Which is what you have used in your jQuery AJAX


Examining the Response Object, data - It's string or an object which is used to carry response from the server. headers - This function is used to get header information. status - This property is used  Convert servlet data into JSON format. How to read JSON file data from the server using agularJs HTTP Get method. Convert servlet data into JSON format.


AngularJS HTTP Get Method ($http.get) with Example, In angularjs we use $http service to send or get data from remote http servers using The response object which we will get after execution of $http.post method  AngularJS is perfect for displaying data from a Database. Just make sure the data is in JSON format.