I am trying to Configure the URL and view to check if javascript is calling the view or not using the ajax call.
I am able to Pass this JSON data using AJAX POST method to the django URL.
But in view its not entering in to the condition if request.is_ajax(): it directly goes to the else part in view.
views.py
@csrf_exempt
def lat_ajax(request):
if request.is_ajax():
if request.method == 'POST':
data = request.POST.all()
print data
return HttpResponse(json.dumps({'data': data}), content_type="application/json")
else :
print "you are not in ajax"
return render_to_response('poll/ajax_test.html', locals())
ajax request
var data1 = JSON.stringify(data);
console.log(data1)
//alert(data1)
alert("hi")
$.ajax({
url: "http://127.0.0.1:8000/poll/lat_ajax/",
type:"POST",
data: data,
contentType:"jsonp",
dataType: "jsonp",
success:function(response){
alert ("resp: "+response.data);
alert("you in ajax")
}
});
return false;
});
This html page is not from the same project. I am using the django project url to pass data and then send that data to one email address without saving it. Is this a cross domain problem? In chrome i am getting the error
XMLHttpRequest cannot load file:///C:/test.everycrave.com/contact/contact.php. Received an invalid response. Origin 'null' is therefore not allowed access. contact.html:1
Resource interpreted as Script but transferred with MIME type text/html: "http://127.0.0.1:8000/poll/lat_ajax/?callback=jQuery18102593700629658997_13…1%40gmail.com&subject=Fwd%3A+DB+design&message=sdfsddfsfds&_=1399357203734". jquery.js:3
send jquery.js:3
p.extend.ajax jquery.js:3
(anonymous function) contact.html:256
p.event.dispatch jquery.js:3
g.handle.h
Please help me out.