Login

Javascript HTTP response

Author:
davep
Posted:
July 26, 2007
Language:
Python
Version:
.96
Score:
-2 (after 4 ratings)

I've been using this along with prototype to make simple ajax calls. In essence you make the calls from the client with...

new Ajax.Request('url',{parameters:{'someparam':$('someparam').value}}); return false;

On the onsubmit event of a form, onclick or whatever. Note that the return false is important to prevent the page from reloading. Sending some javascript to be executed back to the client is then as simple as setting up your view to return:

return HttpJavascriptResponse('alert("boing");')

So, yeah, prototype does the real work and this class does little other than make it clear what our intentions are and reduce the opportunities for typos. But it works for me.

1
2
3
class HttpJavascriptResponse(HttpResponse):
    def __init__(self,content):
        HttpResponse.__init__(self,content,mimetype="text/javascript")

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.