Version 17 (modified by erob@…, 11 years ago) (diff)

updated ajax.js


Here's a simple recipe that worked for me. It is quite inspired by the AjaxDojoFormSub example―and from this tutorial―except that this recipe is using python-cjson to receive JSON-encoded data.

My initial objective was to display validation errors without doing a page-refresh, while using JSON for carrying errors (or whatever else you could possibly imagine) back to the client browser.

The server view

First thing first, lets write the server view, as its really important to make the forms working without the use of Javascript:

import cjson
from django.http import HttpResponse
from django.template import loader, Context

def register(request):
    # Register or reports any (new)form-validation errors using JSON.
    form = ExampleForm(request.POST)
    t = loader.get_template('myform.html')
    if not form.is_valid():
       # Return JSON object containing the errors object.
       if xhr:
          return HttpResponse(cjson.encode(form.errors), mimetype='text/javascript')
          # No JSON here!
          return HttpResponse(t.render(Context(form.errors))
       # Do something when the form has been validated.

The client view

Once the server view has been written, the idea is to write down the client UI (or view, depending whether you're a purist or not.. ;-)

Quite frankly, that step is straight-forward, and looks pretty-much like writing down basic HTML:

<script type="text/javascript">
    djConfig = {
        isDebug: true,
        debugAtAllCosts: false
<script type="text/javascript" src="/media/js/dojo/dojo.js"></script>
<script type="text/javascript">
    dojo.require("dojo.event.*");       // sophisticated AOP event handling
    dojo.require("*");          // for Ajax requests
    dojo.require("dojo.json");          // serialization to JSON
<!-- Here's the client UI script to play with Dojo  -->
<script type="text/javascript" src="/media/js/ajax.js"></script>

The djConfig.isDebug parameter is important for debugging, so you should consider setting it to "true" when inspecting you're application with Firebug or something else.

To avoid loading the Dojo modules twice (for speed), just roll your own Dojo script, and then remove the implied ("baked") modules from the list of required modules above.

Form validating with

Let's complete our application by writing down the core JS file, for doing some-cute-AJAX-related-things-with-Dojo:


function myinit() {
    // connect the event with the good handler
    var myButton = dojo.byId("xBtn1");
    dojo.event.connect(myButton, "onclick", "sendForm");
function sendForm() {
    var fNode = dojo.byId('xForm')
    var fParms =
    var myErrorDiv = dojo.byId('errorBox1')
    var bindArgs = {
        url: ".",
        mimetype: 'text/plain',
        method: "post",
        preventCache: true,
        transport: "XMLHTTPTransport",
        postContent: fParms,
        error: function(type, error, http){
               // Handle error here
               if (http.responseText)
                   // Return the raw server response
                   // (useful when debugging)
               return false
        load: function(type, data, http, kwArgs){
              // Handle "successful" responses here
              var errStr = ''
              if(type == 'load'){
                try {
                 var json = eval( '(' + data + ')' )
                } catch (e) {
                  // not json
                  return false
                for (x in json) 
                    errStr += x + ': ' + json[x] + '<br />'
                myErrorDiv.innerHTML = errStr
    var xmlhttp =
    return xmlhttp

Notice the absence of the formNode param for postContent, and the presence of to parse/encode the form data into a proper format. That is all required for having the XMLHTTPRequest object handled by the Dojo toolkit.

To connect the input submit button with the JS callback, use the following line:

<input type="button" onclick="validate()" id="fooBtn"></input>

Have fun! That recipe should work in Firefox 2 and Opera 9.10.


  • Usability issues: HTML 4.01 vs XHTML 1.0 strict vs No javascript
  • How to display other mimetype(s) when JSON is not used?


Add some notes here.


Attachments (2)

Download all attachments as: .zip

Back to Top