I'm trying to post data to a django view using ajax. I have no errors just there is no data returned. Here is my javascript ajax:
function PostGoal(){
console.log('POSTGOAL!!!')
data_s = {
'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
'goal': { 'name':'gg','box':'sasa' }
}
$.ajax({
method: "POST",
url: "http://127.0.0.1:8000/",//"{% url 'home' %}",//"/",//"{% url 'home' %}",
contentType: 'application/json',
//data: JSON.stringify(data_s),
data: {'QQww': "1"},
//dataType: 'json',//expected type of response
success: function (data) {
console.log('aa'+JSON.stringify(data))
},
error: function(xhr,errmsg,err){
console.log('err: '+JSON.stringify(err)+' msg:'+errmsg)
}
});
}
here is my view function:
def analyzer(request):
if request.method == 'POST':
post_data = request.POST
print(post_data)
print (' ajax:',request.is_ajax())
The boolean request.is_ajax() is ajax is always False request.data or request.POST.data do not exist and same for GET: request.GET is empty. However I can see in the log in the terminal :
[22/Mar/2018 22:15:26] "GET /?QQww=1 HTTP/1.1" 200 9746
so essentially the data are parsed in the url? Thanks in advance.