tags:

views:

29

answers:

2

I am new to ajax and using Django for web development. Now My Template contains : sample.html

<html>
<body>
<script language="javascript" type="text/javascript">
//Browser Support Code
function ajaxFunction(){
        var ajaxRequest;  // The variable that makes Ajax possible!

        try{
                // Opera 8.0+, Firefox, Safari
                ajaxRequest = new XMLHttpRequest();
        } catch (e){
                // Internet Explorer Browsers
                try{
                        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try{
                                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e){
                                // Something went wrong
                                alert("Your browser broke!");
                                return false;
                        }
                }
        }



        // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
                if(ajaxRequest.readyState == 4){
                        document.myForm.time.value = ajaxRequest.responseText;
                }
        }
        ajaxRequest.open("GET", "/showtime/", true);
        ajaxRequest.send(null);
}

</script>



<form name='myForm'>
Name: <input type='text' onBlur="ajaxFunction();" name='username' /> <br />
Time: <input type='text' name='time' />
</form>
</body>
</html>

In views.py my function is :

def showtime(request):
       string = "Ajax Application"
       data = {"string" : string}
       pprint (data)
       return render_to_response("sample.html",data)

Now, The output is not as expected . The template does not receives the response sent by the server What is wrong with the code ?

A: 
  1. try if /showtime/ works as expected when you type in your browser!
  2. using a js framework like jquery will save you time and energy implementing ajax stuff, eg. see this tutorial http://lethain.com/entry/2007/dec/11/two-faced-django-part-5-jquery-ajax/
  3. there are also some django-built-ins that you can use, eg request.is_ajax to verify if the request is really comng from ajax!
lazerscience
is_ajax is good, but you lose the ability to just hit the URL in your browser to check the response. I only use is_ajax when I want the same view to respond differently to an ajax request.
Chris Lawlor
+2  A: 

If you're trying to populate the text-field with AJAX returned string, you should not use render_to_response. Just return the string.

def showtime(request):
   s = "Ajax Application"
   print "Returning %s"%s #is this line executed?
   return HttpResponse(s, mimetype="text/plain")
Amarghosh
you always should return a HttpResponse object like return HttpResponse("Ajax Application", mimetype="text/plain")
mawimawi
@mawimawi Corrected
Amarghosh
again: DON'T use the print statement. in some server setups the print statement causes a HTTP 500 error (as far as I remember in wsgi or fcgi this is the case). Just return the HttpResponse.
mawimawi
@mawimawi I'm not suggesting he use this in production code; just trying to make sure the function indeed gets called. OP hasn't shown his `urls.py` yet.
Amarghosh
Thanks a lot all of you for replying Its working
tazim