4

Suppose I have an AJAX function:

function callpage{
$.ajax({
    method:"get",
    url:"/abc/",
    data:"x="+3
    beforeSend:function() {},
    success:function(html){
       IF HTTPRESPONSE = "1" , ALERT SUCCESS!
    }
    });
    return false;
}
}

When my "View" executes in Django, I want to return HttpResponse('1') or '0'.

How can I know if it was successful, and then make that alert?

DarkAjax
  • 15,955
  • 11
  • 53
  • 65
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

2 Answers2

16

The typical workflow is to have the server return a JSON object as text, and then interpret that object in the javascript. In your case you could return the text {"httpresponse":1} from the server, or use the python json libary to generate that for you.

JQuery has a nice json-reader (I just read the docs, so there might be mistakes in my examples)

Javascript:

$.getJSON("/abc/?x="+3,
    function(data){
      if (data["HTTPRESPONSE"] == 1)
      {
          alert("success")
      }
    });

Django

#you might need to easy_install this
import json 

def your_view(request):
    # You can dump a lot of structured data into a json object, such as 
    # lists and touples
    json_data = json.dumps({"HTTPRESPONSE":1})
    # json data is just a JSON string now. 
    return HttpResponse(json_data, mimetype="application/json")

An alternative View suggested by Issy (cute because it follows the DRY principle)

def updates_after_t(request, id): 
    response = HttpResponse() 
    response['Content-Type'] = "text/javascript" 
    response.write(serializers.serialize("json", 
                   TSearch.objects.filter(pk__gt=id))) 
    return response           
Community
  • 1
  • 1
Tom Leys
  • 18,473
  • 7
  • 40
  • 62
  • 3
    Someone gave me a good answer relating to this a couple weeks ago: http://stackoverflow.com/questions/1457735/django-models-are-not-ajax-serializable – theycallmemorty Oct 06 '09 at 20:45
  • 1
    An alternative way to return the json data,from a model... def updates_after_t(request, id): response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(serializers.serialize("json", TSearch.objects.filter(pk__gt=id))) return response – ismail Oct 06 '09 at 23:05
2

Rather than do all this messy, low-level ajax and JSON stuff, consider using the taconite plugin for jQuery. You just make the call to the backend and it does the rest. It's well-documented and easy to debug -- especially if you are using Firebug with FF.

Peter Rowell
  • 17,605
  • 2
  • 49
  • 65
  • 1
    I'm not sold on these "create xml and magic happens" type things. Much better to point someone to backbone.js or something similar. – peregrine Jul 21 '11 at 02:56
  • @peregrine: Have you *used* jquery-taconite? It's not magic, it's just lots of irritating details dealt with in a clean, predictable manner. The code itself is quite readable, as is most everything from malsup that I've used. I look at taconite as being no more magic than using a compiler for C or an interpreter for Python -- they both make life easier for me. I programmed in assembler for years and understand what is happening right down to the bare iron (or silicon), but that doesn't make me like it. I use jQuery for many things so that is a given for most any project I'm working on. – Peter Rowell Jul 21 '11 at 03:17