【发布时间】:2017-10-30 17:49:48
【问题描述】:
这是我使用 NodeJS 的后端代码,我试图通过它发送 JSON 数据。 -
con.query(sql, function(err, result, fields){
if(err) throw err;
res.setHeader('Content-Type', 'application/json');
var Dict = {"records":result};
res.end(JSON.stringify(Dict));
});
这是我使用 AngularJS 的前端代码,我试图通过它在浏览器上显示 JSON 数据-
<body>
<div ng-app='myApp' ng-controller="MyCtrl">
<p ng-model="rows">
{{ rows }}
</p>
</div>
<script>
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', function($scope,$http){
$http.get("http://localhost:1208").then(function(response){
$scope.rows = response.data.records;
});
});
</script>
</body>
但是当我在浏览器上运行这个 Angularjs 文件代码时,它什么也没显示。谁能帮我解决这个问题?我想在浏览器上显示我的 JSON 数据。
【问题讨论】:
-
你试过在前端安慰响应并检查吗?
-
将
<p ng-model="rows"> {{ rows }} </p>替换为<pre>{{rows|json}}</pre>以便于调试。