Forums

page request and AJAX

How do you turn off normal page response/request and use AJAX response/request instead?

Hi Moc,

Can you clarify what you are trying to do? And maybe which web framework you are using? You typically make AJAX requests from the client side via Javascript.

Using Django can I do a normal page request/response but using an AJAX framework to perform page request/response?

Are you using something like Dojo to manage your client side Javascript? Also: you need to make sure that any requests you make are within the same domain (this is a Javascript restriction).

(If you've not used Dojo to manage this kind of thing, I highly recommend it)

I guess my confusion is that a response to an AJAX request (initiated by the visitors browser) is just an HTTP response. Perhaps with an XML mimetype?

I wanted to use jquery to handle all http request. Can it be done using django ?

I think the answer is yes. Django can return both "normal" HTTP responses for the web browser, or "ajax" responses for jquery. The only difference is that jquery expects JSON for its responses. So, in your django views you could use something like this:

import json
from django.http import HttpResponse

def an_ajax_view(request):
    # do whatever processing you want for your view, then return, eg
    data_for_jquery = {'a key': 'a value', 'another key': 'another value'}
    return HttpResponse(json.dumps(data_for_jquery), mimetype="application/json")