【发布时间】:2017-09-17 21:45:38
【问题描述】:
我正在尝试获取一个基本的 angular.js 页面,以显示使用来自 node.js 服务器的 http get 请求检索到的信息。但是,我遇到了 Angular 无法识别我正确获取的 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 文件的相关 sn-ps
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"}]}
任何关于我做错了什么的帮助或关于我在哪里可以找到更多信息的提示将不胜感激。
【问题讨论】:
标签: javascript angularjs json node.js