AngularJS:如何将url传递到$http POST

时间:2021-10-02 19:42:43

I am having trouble with my http POST request in AngularJS. The error I am getting is:

我在AngularJS中遇到了http POST请求问题。我得到的错误是:

POST http://localhost:8080/ 403 (Forbidden). 'rating-add' is a named url

So I don't know why it doesn't allow this. This is the template:

我不知道为什么不允许这样。这是模板:

<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>

And the urls.py:

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 个解决方案

#1


2  

You need to append a leading slash like this -

你需要添加一个这样的斜杠。

url : '/rating/add'

Check out the docs here.

看看这里的文档。

You should print out your routes from Django to find out where to post.

您应该打印出您从Django的路线,以找出在哪里发布。

#1


2  

You need to append a leading slash like this -

你需要添加一个这样的斜杠。

url : '/rating/add'

Check out the docs here.

看看这里的文档。

You should print out your routes from Django to find out where to post.

您应该打印出您从Django的路线,以找出在哪里发布。