1
How do I submit a post on Angular? I ran a test here and requires you to report to PK:
index.controller('EditController', function($scope, $http) {
$scope.save = function() {
var in_data = { name: $scope.Job.name, description: $scope.Job.description };
$http.put('job/5/', in_data)
.success(function(out_data) {
console.log("Success!");
}).
error(function(data, status, headers, config) {
console.log(status, data);
});
};
});
The problem would be specifically here:
$http.put('job/5/', in_data)
Obviously the id could not go in the url. How to send this post without this id?
When sending without id this is the error:
405 METHOD NOT ALLOWED
{"detail": "Method 'PUT' not allowed."}
My urls.py:
url(r'^job/(?P<pk>[0-9]+)/$', views.job_detail, name="add_job"),
My views.py:
try:
job = Job.objects.get(pk=pk)
except Job.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
elif request.method == 'PUT':
serializer = JobSerializer(job, data=request.DATA)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Edited:
I was able to change the "PUT" method to "POST" in views.py
and using $http.post()
when making the requisition.
If you have found the solution, create an answer, do not put within the question as you did. Welcome.
– Paulo
Sorry for the mistake, I’ll follow the recommendation next time. Hug!
– LinuxMan