Ajax with JQuery on the Django Dev Server

0

I am putting together an Ajax request with JQuery (1.5) on a Django (1.2.5) site. I am working on the Dev Server currently because I plan on doing demonstrations before a full production roll out.

The javascript code in the template:

function postTest()
{
        $.post("/get_elections", { csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val() }, alertResult);

}

function alertResult(result)
{
    alert(result);
}

How it's called:

<a href="" onclick='postTest()'>Link</a>
{% csrf_token %}

The Ajax view:

def get_selections(request):
if request.is_ajax():
    if request.method == 'GET':
        message = "This is an XHR GET request"
    elif request.method == 'POST':
        message = "This is an XHR POST request"
        # Here we can access the POST data
        print request.POST
else:
    message = "No XHR"
    return HttpResponse(message)

The Index View:

def index(request):
    polylist = [] 
    return render_to_response('test.html', {'polylist': polylist},context_instance=RequestContext(request))

The URLConf:

(r'^gi_prototype/', 'world.views.index'),
(r'^get_selections$', 'world.views.get_selections')

Relevant settings:

APPEND_SLASH = 'false'

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

When I click on "Link" in the page, the page reloads and an exception is thrown. The exception, both in Firebug and in Firefox Error Console:

Error: uncaught exception: [Exception... "Component returned failure code: 0x80040111 >(NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.getAllResponseHeaders]" nsresult: "0x80040111 >(NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://localhost:8000/site_media/jquery-1.5.js :: anonymous :: line 7207" data: no]

The Django Dev Server provides this output (token replaced with question marks by me):

<QueryDict: {u'csrfmiddlewaretoken': [u'??????????????????????????????']}>
[22/Feb/2011 16:31:23] "POST /get_selections HTTP/1.1" 200 27
[22/Feb/2011 16:31:23] "GET /gi_prototype/ HTTP/1.1" 200 970

After several hours of searching, I have begun to think this is simply a limitation of the Django Dev Server, but that seems foolish. Any other suggestions would be very helpful. Thanks in advance.

javascript
python
ajax
django
asked on Stack Overflow Feb 22, 2011 by MrOodles • edited Feb 22, 2011 by MrOodles

1 Answer

2

So I discovered the answer after many long hours of research, and many would do well to learn from my pain.

The problem lay here:

<a href="" onclick='postTest()'>Link</a>

XMLHttpRequests, no matter what their wrapper, cannot be initiated through an onclick in a link tag with an href. I merely changed the code to this:

<form><input type='button' value='Link' onclick='postTest()'/></form>

And it worked perfectly. No server problem. No AJAX errors. Just a matter of using the correct markup.

Heed the warnings of my tale of woe, lest ye suffer my same fate!

answered on Stack Overflow Feb 23, 2011 by MrOodles

User contributions licensed under CC BY-SA 3.0