使用angular js的Json rest客户端无法填充数据(空值)
Json rest client using angular js cannot populate data (Empty value)
请咨询
我正在使用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;
或者只需确保你的标题设置正确,你不应该这样做。
相关文章:
- 如何在Highchart中动态填充数据
- 使用jquery在选择框中的选项中填充数据
- 使用jquery、javascript重新填充数据
- 如何从 API 填充数据并加载到下拉列表中
- 通过遍历JSON文件来填充数据列表选项
- 在引导模式中,点击相应的datatables行中的按钮,填充数据
- 当选择District时,PHP将从数据库中填充数据
- 从角度工厂返回数据,返回对象后填充数据
- 高图表 - 从数组填充数据
- 在承诺函数从 pouchdb 填充数据之前触发 Ionic/Angular $watch的问题
- 无法用同位素填充数据
- 使用 php 发送 json 填充数据表
- JQuery - 在表单中填充数据的正确方法
- 翡翠选择字段填充数据
- C# MVC 填充数据表中的下拉列表组
- 自动填充数据被javascript的onchange事件覆盖
- 如何在html中只使用ng repeat来填充数据,而不使用repeat html's元素
- 用Firebase和Angular填充数据表
- 通过JS Ajax调用问题填充数据
- 使用两个ajax调用在两个html表中填充数据