0
我使用Django REST框架。 寫作API的工作,如果你手動輸入的數據在頁面上:http://atata.com/en/api/v1/add_comment/告訴我如何向api發出POST請求。使用restangular
views.py(API)
class AddComment(generics.CreateAPIView):
"""
Create new comment.
Example API structure:
{
"post_pk": 1,
"name": "MyName",
"email": "[email protected]",
"text": "This is text message!"
}
"""
model = Comment
serializer_class = CommentSerializers
和API的作品。
controllers.js
var app = angular.module('controllers', []);
app.controller('CommentAddCtrl', function($scope, Restangular){
$scope.logComment = function(mail, name, text){
var url = window.location;
var pk = url.hash.split('$pk:')[1];
var url_add_comment_api = './en/api/v1/add_comment';
if (mail === undefined || name == undefined || text === undefined)
alert ('Error!');
else {
var comment = {
post_pk: pk,
name: name,
email: mail,
text: text
};
this.add_comment = Restangular.all(url_add_comment_api).post(comment);
}
};
});
當控制檯收到錯誤: POST http://atata.com/en/api/v1/add_comment/ 403(禁止)
你是否傳遞和API密鑰?這可能是爲什麼它說「403(FORBIDDEN)」,如果它缺少 –
你是否遵循[CSRF文檔](http://django-angular.readthedocs.org/en/latest/csrf-protection.html)? – mariodev