I am trying to send my JavaScript this.click variable from my .js file to my Django view class Click using Ajax and getting the following error message...
AttributeError: 'WSGIRequest' object has no attribute 'data'
Here is my view...
class Clicks(View):
def post(self, request):
clicks = request.data.get('clicks')
return JsonResponse({'status': True})
Here is my .js files ajax...
var image_movement = function(){ //Handles movement of tug of war image
this.total = 18
this.clicks = 0
this.move = function(num){ //Adds or subtracts one from total depending on which player calls it
if(this.total == 0){
$('#onewon').show();
$.ajax({
headers: {
'Content-Type':'application/json',
'X-CSRFToken': getCookie('csrftoken')
},
url: 'click',
type: "POST",
data: {clicks: this.clicks},
success:function(response){
alert("success: " + response);
},
error:function (xhr, textStatus, thrownError){
alert("failure: " + xhr.statusText);
}
}).done(function (response) {
alert("end");
});
Here is my .js files function for obtaining cookie to give to ajax header...
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
}
Here is my url...
url(r'^click$', Clicks.as_view()),
Things that I have tried...
1) Using clicks = request.DATA.get('clicks') instead of clicks = request.data.get('clicks') (get the same error as above)
2) Switching to own specific class based view to avoid any interference.