25👍
I don’t know what happened to you. I tested and got a good message: This is ajax
. My code:
urls.py
url(r'^$', 'myapp.views.home', name='home'),
url(r'^ajax_test/$', 'myapp.views.ajax_test', name='ajax_test'),
views.py
def home(request):
return render_to_response('home.html', {},
context_instance=RequestContext(request))
def ajax_test(request):
if request.is_ajax():
message = "This is ajax"
else:
message = "Not ajax"
return HttpResponse(message)
templates/home.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function () {
$.get("/ajax_test/", function (data) {
alert(data);
});
});
</script>
</body>
</html>
21👍
Looks like is_ajax
just checks the HTTP_X_REQUESTED_WITH
(looks like X-Requested-With
in HTTP) header. If it equals XMLHttpRequest
, we have an ajax query.
So it’s pretty easy now to trace the fate of this header:
-
Look at the initial HTTP request (using browser’s debug plugin, Wireshark, tcpdump or any other tool you prefer) and see if it has a correct
HTTP_X_REQUESTED_WITH
header. If it doesn’t, the problem is with jQuery (or your script). -
The header can be lost during redirection, as Daniel Roseman said (haven’t seen this myself, but almost sure it’s possible). You’ll see two (or more) chained HTTP requests in this case, and one of them will lack the
HTTP_X_REQUESTED_WITH
header. In this case, the problem could be with the webserver’s config or redirection itself (you may have to get rid of it or manually relay the header if redirection response is generated by your code). -
Also, HTTP proxies can cut headers. In this case you may look at the HTTP request on the server side via traffic inspector or
request.environ
(pretty garbled with environment vars, but HTTP headers are also there).
- [Django]-How to use environment variables with supervisor, gunicorn and django (1.6)
- [Django]-How do I do a not equal in Django queryset filtering?
- [Django]-What is more efficient .objects.filter().exists() or get() wrapped on a try
6👍
Probably you’ve got APPEND_SLASH turned on (the default), and Django is redirecting from /ajax_test
to /ajax_test/
but not preserving the relevant header. Better to request the version with the slash in the first place.
- [Django]-Django queryset filter – Q() | VS __in
- [Django]-How do I run tests for all my Django apps only?
- [Django]-How to make Django serve static files with Gunicorn?
1👍
Adjusted my code to the following:
views.py:
def index(request):
return render_to_response('index.html',
locals(), context_instance=RequestContext(request))
def ajax_test(request):
if request.is_ajax():
message = "This is ajax"
else:
message = "Not ajax"
return HttpResponse(message)
urls.py:
urlpatterns = patterns('',
# root
url(r'^$', views.index, name='index'),
# ajax
url(r'^ajax_test/$', views.ajax_test, name='ajax_test'),
)
template:
...
<script type="text/javascript">
$(document).ready( function() {
$.get("/ajax_test/", function(data) {
alert(data);
});
});
</script>
- [Django]-Vagrant + Chef: Error in provision "Shared folders that Chef requires are missing on the virtual machine."
- [Django]-Where to put Django startup code?
- [Django]-Django cannot find static files. Need a second pair of eyes, I'm going crazy
-2👍
Try with other version of jQuery… it possible that the problem was in the js…
For example:
http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js
- [Django]-Passing data from Django to D3
- [Django]-Foreign Key Django Model
- [Django]-How to aggregate (min/max etc.) over Django JSONField data?
-2👍
In your .get
call, you must specify the CSRF Token. I don’t know if this is the problem, but should help.
.get(..., function(data) {
(...)
data: { csrfmiddlewaretoken: '{{ csrf_token }}',
(...)
})
- [Django]-LEFT JOIN Django ORM
- [Django]-How to produce a 303 Http Response in Django?
- [Django]-Django: allow line break from textarea input