I am trying to make an ajax call from an external javascript file.
ajax.js
function ajax() {
setInterval(function() {
$.ajax({
url : "/ajax_get.html",
type : "POST",
dataType: "json",
data : {
client_response : 'Y',
csrfmiddlewaretoken: '{{ csrf_token }}',
},
success : function(json) {
alert('Working');
},
error : function(xhr,errmsg,err) {
alert(xhr.status + ": " + xhr.responseText);
}
});
alert('Failing');
return false;
}, 5000);
}
Inclusion in the template
<script>
ajax();
{% csrf_token %}
</script>
Views.py
def ajax_request(request):
if request.POST.has_key('client_response'):
x=request.POST['client_response']
response_dict={}
response_dict.update({'server_response':'working'})
return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
else:
return render_to_response('ajaxexample.html', context_instance=RequestContext(request))
I tried to look it up but the only examples I faced where ajax calls from Forms. What I am trying to achieve is to append the output from the ajax call to the file. The console tells me that there is a syntax error regarding the csrf token inside the script tag.
You've placed the CSRF token on its own inside the script tag in the template: that doesn't make sense, as it's not valid Javascript by itself.
Also, although it's not a syntax error, you've used Django template variable syntax inside the JS file itself, which won't be parsed - so the parameter will be sent as the raw string "{{ csrf_token }}"
.
However the Django documentation contains full instructions for using CSRF with Ajax, so you should follow those.
I am trying to make an ajax call from an external javascript file.
ajax.js
function ajax() {
setInterval(function() {
$.ajax({
url : "/ajax_get.html",
type : "POST",
dataType: "json",
data : {
client_response : 'Y',
csrfmiddlewaretoken: '{{ csrf_token }}',
},
success : function(json) {
alert('Working');
},
error : function(xhr,errmsg,err) {
alert(xhr.status + ": " + xhr.responseText);
}
});
alert('Failing');
return false;
}, 5000);
}
Inclusion in the template
<script>
ajax();
{% csrf_token %}
</script>
Views.py
def ajax_request(request):
if request.POST.has_key('client_response'):
x=request.POST['client_response']
response_dict={}
response_dict.update({'server_response':'working'})
return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
else:
return render_to_response('ajaxexample.html', context_instance=RequestContext(request))
I tried to look it up but the only examples I faced where ajax calls from Forms. What I am trying to achieve is to append the output from the ajax call to the file. The console tells me that there is a syntax error regarding the csrf token inside the script tag.
You've placed the CSRF token on its own inside the script tag in the template: that doesn't make sense, as it's not valid Javascript by itself.
Also, although it's not a syntax error, you've used Django template variable syntax inside the JS file itself, which won't be parsed - so the parameter will be sent as the raw string "{{ csrf_token }}"
.
However the Django documentation contains full instructions for using CSRF with Ajax, so you should follow those.
0 commentaires:
Enregistrer un commentaire