jquery - AJAX Post request from Javascript to Python (Django, views.py) -


i trying send post request javascript views.py using ajax. however, getting 403 error stating csrf token not present.

in order resolve issue followed this link , included function in javascript. however, confused next step should be.

any assistance appreciated. thanks!

simply add somewhere in template:

{% csrf_token %} 

then in js file should smth this:

var csrf_token;  var sendsomeajax = function(target) {     var requesturl = $(target).data('url');      return $.ajax({         url: requesturl,         type: 'post',         headers: {             'x-csrftoken': csrf_token         },         datatype: 'json'         // can pass 'data: ' here     }) };  $(function() {     csrf_token = $('input[name="csrfmiddlewaretoken"]').val();      var target = $('.someselectorwhereyouhavepasseddataurltoyourview');     // example in template <a href="#" data-url="{% url 'app_url_namespace:view_url_name' %}"</a>      sendsomeajax(target).success(function(data) {         // smth data =)     }); }); 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -