Help needed with POST and DELETE requests using Ajax! I have set up the methods in my class but something seems to be going wrong.
Any assistance would be much appreciated 😃
urls.py:
path('<section>/add_wish/<slug>/', views.AddToWishlistView.as_view(), name='add_to_cart'),
My view :
class AddToWishlistView(LoginRequiredMixin, View):
model = Wishlist
http_method_names = ['POST']
def POST(self, request, *args, **kwargs):
wished_product = get_object_or_404(Product, slug=self.kwargs['slug'])
new_item = self.model.objects.get(customer = self.request.user)
new_item.product.add(wished_product)
return HttpResponse(status=201)
And here's the Ajax code :
$('.buy').click(function(e){
e.preventDefault();
let _this = $(this);
var slug = _this.children().data('id');
var section_slug = _this.data('section');
$.ajax({
type : 'POST',
url : '../'+section_slug + '/add_wish/' + slug + '/',
success: function(data){
if(data.success = true){
_this.addClass('clicked');
}
},
async : false,
error : function(data){
console.log("ERROR");
console.log(data);
alert('LOOSERR');
}
})
});