json AngularJS:从服务器读取响应数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27114940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:38:49  来源:igfitidea点击:

AngularJS: read response data from server

jsonangularjs

提问by sebvst

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

我有一个应该解决的问题,但我无法弄清楚我做错了什么。我data通过$http请求收到。

alert(data)

gives me object object

给我 object object

alert(data.response)

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

给我 {"id":"123456","post_id":"12345"}

alert (data.response.id)

gives me undefined

给我 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?

我的问题:我想拿到身。为什么最后一个表达式给我 undefined 而不是 ID?我是否必须以某种方式转换数据?

I am thankful for any hints!

我很感激任何提示!

回答by sylwester

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

看起来您的 data.response 是一个字符串。您用于angular.fromJson将其转换为对象,即:

$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>

回答by Abdallah Okasha

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.

简单地这里 myRes 有这个请求的响应数据。

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

我们可以使用 angular 中的表达式语法将其显示在 HTML 文件中

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

回答by Umasankar Patra

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);
  });
});