AngularJS:如何将网址传递给$ http POST

时间:2016-04-23 01:52:08

标签: angularjs django

我在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'),
]

1 个答案:

答案 0 :(得分:2)

你需要附加一个这样的前导斜杠 -

url : '/rating/add'

查看文档here

  

您应该从Django打印路线以找出要发布的位置。