user3251349 user3251349 - 7 months ago 24
Javascript Question

AngularJS: Unexpected identifier $http POST

I am having trouble with my http POST request in AngularJS. The function call to $http is giving me a SyntaxError: Unexpected identifier. The unexpected identifier is referring to the line url: 'rating-add' in my $http call. 'rating-add' is a named url, so I don't know why it doesn't know 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:

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

Answer

You are missing ,. change this:

$http({
          method: 'POST'
          url: 'rating-add'
          data: data
        });

into:

$http({
      method: 'POST',
      url: 'rating-add',
      data: data
    });