【发布时间】:2015-07-12 15:26:31
【问题描述】:
我是“Angularjs 和 Nodejs”组合的新手,我正在尝试制作一个简单的 crud 应用程序。问题是,当我在 Angular 中使用 $http.get 时,它不会进入后端。
这是我的代码:
server.js (nodejs)
var connection = util.getDBConnection();
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
//routes = require('./routes/routes')(app, connection);
app.get('/', function(req, res) {
console.log("Backend");
connection.query("SELECT * FROM user_info;", function(err, rows){
if(err){
res.redirect('/error');
console.log("Error in the database: " + err);
}
res.end(JSON.stringify(rows));
console.log("Connected to the Database!");
});
});
angularmodule.js
var app = angular.module('two_way', ['ngRoute']);
app.controller('two_way_control', function($scope, $http){
$scope.message = "Hello from controller";
$http.get('/').success(function(data){
console.log("http get");
console.log(data);
$scope.profile_pictures = data;
});
});
console.log(data) 返回整个 index.html
index.html
<body>
<div id="container" ng-app="two_way" ng-controller="two_way_control">
<div class="row" ng-repeat="data in profile_pictures">
{{message}}
<div class=".col-sm-6 .col-md-5 .col-lg-6">
<h4>User Say's</h4><hr>
<p>
This is a Demo feed. It is developed to demonstrate Two way data binding.
</p>
{{ data.profile_picture }}
<img src="{{data.profile_picture}}">
</div>
</div>
</div>
</body>
但是这段代码返回的是json格式的mysql数据:
app.get('/load', function(req, res) {
console.log("Backend");
connection.query("SELECT * FROM user_info;", function(err, rows){
if(err){
res.redirect('/error');
console.log("Error in the database: " + err);
}
res.end(JSON.stringify(rows));
console.log("Connected to the Database!");
});
});
请帮忙。在此先感谢,抱歉英语不好。
【问题讨论】:
标签: javascript mysql angularjs node.js