【发布时间】:2016-10-25 11:01:20
【问题描述】:
问题应该足够清楚,我的代码应该会显示问题。有什么问题可以在下方评论。
app.js
var express = require('express');
var app = express();
app.use(express.static('public'));
var characters = {"Griffins":
[{"Name":"Lois Griffin",
"Gender":"Female",
"Role": "Mother"},
{"Name":"Chris Griffin",
"Gender":"Male",
"Role": "Son"},
{"Name":"Meg Griffin",
"Gender":"Female",
"Role": "Daughter"}]
};
app.get("/characters", function(req, res) {
res.send(characters);
})
app.listen(9000, function(){
console.log('Running');
});
angular.js
app.controller('listCtrl',function($scope, $http){
$scope.characterData = function() {
$http.get("/characters").then(function(data) {
console.log(data, 'success')
},function(data) {
console.log("data, failure.")
})
}
})
错误
Failed to load resource: the server responded with a status of 404 (
Object "failure."
对象出错
Object -
config : Object
data : "Cannot GET /characters↵"
headers : function (d)
status : 404
statusText : "Not Found"
__proto__ : Object
注意:使用 $http.get('characters.json')... 时,我可以从名为 'character.json' 的文件中获取数据。
【问题讨论】:
-
试试 res.json(characters);而不是 res.send
-
不幸的是仍然收到同样的错误
-
您是否尝试直接从浏览器访问 /characters?你也可以试试 cUrl。
标签: javascript angularjs node.js express get