使用angular js的Json rest客户端无法填充数据(空值)

Json rest client using angular js cannot populate data (Empty value)

本文关键字:填充 数据 空值 客户端 js angular Json rest 使用      更新时间:2023-09-26

请咨询

我正在使用angular js创建带有json输出的rest客户端,尽管已经收到了rest数据,但它从未正确填充到view html中。

在角度控制器上是否定义了错误的$scope数据?

下面是json rest输出数据

{
 "Job": [
    {
        "id": "1",
        "prize": "car"
    }
       ]
}

以及 名为"some.js"的javascript控制器

var app = angular.module("MyApp", []);
app.controller("PostsCtrl", function($scope, $http) {
$http.get('jsonURL').
success(function(data, status, headers, config) {
  $scope.posts = data.Job;
 }).
error(function(data, status, headers, config) {
 });
});

以及 html视图

<!doctype html>
<html ng-app="MyApp">
<head>
    <title>Test AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
    <script src="some.js"></script>
</head>
<body>
    <div ng-controller="PostsCtrl">
        <ul ng-repeat="post in posts">
            <p>The ID is {{post.id}}</p>
            <p>The prize is {{post.prize}}</p>
        </ul>
    </div>
</body>
</html>

我觉得还可以。您可能需要检查的一件事是,http响应的内容类型标头为application/json,否则angular将无法正确解析数据。

试试这个解决方案:

var jsonResponse = eval('('+data+')');
$scope.posts = jsonResponse.Job;

或者只需确保你的标题设置正确,你不应该这样做。