我在 AngularJS 中处理 http POST 请求时遇到问题。我收到的错误是:
POST http://localhost:8080/ 403 (Forbidden). 'rating-add' is a named url
所以我不知道为什么它不允许这样做。这是模板:
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http){
$scope.ratings = [];
var data = $scope.myForm = {beer_name:'', score:'', notes:'', brewer:''};
$scope.buttonClick = false;
$scope.is_clicked = function() {
$scope.buttonClick=true;
console.log($scope.buttonClick)
}
$scope.submit_to_form = function() {
$http({
method: 'POST'
url: 'rating-add'
data: data
});
}
})
</script>
还有 urls.py:
from django.conf.urls import url
from django.contrib import admin
from ratings.views import home, RatingCreate, delete, edit
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', RatingCreate.as_view(), name='rating-home'),
url(r'rating/add/$', RatingCreate.as_view(), name='rating-add'),
url(r'rating/delete/(?P<row_id>[0-9]+)/$', delete , name='rating-delete'),
url(r'rating/edit/(?P<row_id>[0-9]+)/$', edit , name='rating-edit'),
]
最佳答案
您需要附加一个像这样的前导斜杠 -
url : '/rating/add'
查看文档 here .
You should print out your routes from Django to find out where to post.
关于AngularJS:如何将 url 传递给 $http POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36805725/