我试图通过Express,Mongoose和Angular $resource
从MongoDB服务器检索JSON数据。虽然服务器显然返回正确的数据,但总是调用错误函数并显示奇怪的错误。
这是(某种程度上)控制器:
.controller('DefaultContentController',
['$scope',
'$location',
'pageFactory',
function($scope, $location, pageFactory) {
var path = $location.path().split('/');
pageFactory.getPages().get({slug:path[1]})
.$promise.then(
function(response) {
console.log(response);
},
function(response) {
console.log("Error: " + response.status);
console.log("Status text: " + response.statusText);
}
);
}])
pageFactory.getPages
定义为
this.getPages = function(){
return $resource("http://localhost:3000/pages/:slug", {});
};
使用评估为packages
标记的位置执行此操作会产生以下结果:
Error: -1
Status text:
GET /pages/packages 200 2.683 ms - 303
如果我在浏览器中输入该位置,则会显示正确的JSON响应,因此显然服务器本身正常工作。原始响应(由Firefox HttpRequrester扩展记录)是
GET http://localhost:3000/pages/packages
-- response --
200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 305
Etag: W/"131-BTgYonvS22Ju7nZNcmxv0w"
Date: Thu, 24 Mar 2016 12:44:43 GMT
[{"_id":"56f3c58ebbea0d5a0e39630a","updatedAt":"2016-03-24T10:46:38.463Z","createdAt":"2016-03-24T10:46:38.463Z","slug":"packages","title":"Packages","html":"<p>As long as there is no auto-generated package documentation this page will simply contain a list of available packages.</p>","__v":0,"tabs":[]}]
有什么想法我可以进一步调查吗?