我有一个Django应用程序,它使用Django rest框架公开数据。我可以访问json格式的对象,如下所示:
curl http://localhost:8000/cemeteries/
或
curl http://localhost:8000/cemeteries/2/
我现在正在尝试使用AngularJS来访问数据。我选择了restangular,这似乎很简单。我已经将restangular配置为:
demoApp.factory('CbgenRestangular', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://localhost:8000');
});
});
然后在我的控制器中:
demoApp.controller('SimpleController', function($scope, simpleFactory, CbgenRestangular, $location){
$scope.customers = [];
CbgenRestangular.all("cemeteries/").getList().then(function() {
console.log("All ok");
}, function(response) {
console.log("Error with status code", response.status);
});
问题是,在我的Django日志中,我总是看到一个HTTPOPTIONS请求:
[14/Aug/2013 12:24:12] "OPTIONS /cemeteries/ HTTP/1.1" 200 10245
在Firebug中,它显示"状态代码为0的错误"。
知道我做错了什么吗?提前感谢您的帮助!
谨致问候,Phil
您看到OPTIONS
请求的原因可能是浏览器发出了飞行前CORS请求。如果是这样的话,你可能想看看https://github.com/ottoyiu/django-cors-headers众所周知,它与REST框架配合得很好。