json数据未被识别为行(angular.js)

时间:2017-04-21 03:21:11

标签: javascript angularjs json node.js

我正在尝试获取一个基本的angular.js页面,以显示使用来自node.js服务器的http get请求检索的信息。然而,我遇到了角度无法识别json的问题我正在得到正确的或者沿着这些线条的东西。以下是index.html

<!DOCTYPE html>
<html >
<script 
  src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl">
<h1>{{names}}</h1>
<table>
<tr ng-repeat="x in names">
    <td>{{ x.title }}</td>
    <td>{{ x.pBody }}</td>
    <td>{{ x.category }}</td>
    <td>{{ x.url }}</td>
</tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://127.0.0.1:8081/process_get")
.then(function (response) {$scope.names = response.data;});
});
</script>

然后这是来自node.js app.js文件的相关片段

var queryString = 'SELECT * FROM posts';
var retRows;
connection.query(queryString, function(err, rows, fields) {
if (err) throw err;
    console.log(rows);
    retRows = rows;
});
app.get('/process_get', function (req, res) {
// Prepare output in JSON format
console.log(response);
res.end(JSON.stringify(retRows));
})

最后,json数据显示如下,我认为应该没问题?

{"retRows":[{"id":1,"title":"global work and travel co.","pBody":"One of the 
worlds leading and largest youth travel 
brands","category":"Travel","url":"https://globalworkandtravel.com"},
{"id":2,"title":"travel.com.au","pBody":"One destination endless 
possibilities","category":"Travel","url":"https://www.travel.com.au"},
{"id":3,"title":"Flight Centre","pBody":"Australias leading travel 
agent","category":"Travel","url":null},{"id":4,"title":"The skinny 
confidential","pBody":"lifestyle 
blog","category":"Lifestyle","url":"https://www.theskinnyconfidential.com"},
{"id":5,"title":"three years of travel","pBody":"Three years of travel and 
adventure from around the 
world","category":"Video","url":"https://www.youtube.com/watch?
v=wJF5NXygL4k"}]}

任何有关我正在做错的帮助或有关我可以在哪里找到更多信息的提示都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您必须在ng-repeat指令中引用retRows的{​​{1}}属性:

names