python - AJAX in Django with jQuery quite confusing -


i'm jquery noob here, please help! much appreciated!

my urls.py: url('^xhr_test/$','posts.views.xhr_test'),

my views.py:

def xhr_test(request): if request.is_ajax():     message = "hello ajax" else:     message = "hello" return httpresponse(message) 

so, simple. setup works if 1 of 2 versions of html page.

html version 1 success:

<script type="text/javascript"> $(document).ready(function() {         $.get("xhr_test", function(data) {            alert(data);        }); }); </script> 

that's that. ajax request made whenever page done loading. works, , nice alert box says "hello ajax"

now html version 2 not work:

$(document).ready(function() {    $("a").click(function() {        alert("hi");        $.get("xhr_test", function(data) {            alert(data);        });    }); }); <a href="">click here</a> 

so when click "click here" alert "hi" in it, , nothing. nothing seems sent nor received, because don't see in firebug. tried changing request url "xhr_test" "myapp/xhr_test" , "/myapp/xhr_test" no avail.

what doing wrong?

thank much!






try adding event.preventdefault(); code; e.g.:

$(document).ready(function() {    $("a").click(function() {        event.preventdefault();        alert("hi");        $.get("xhr_test", function(data) {            alert(data);        });    }); }); <a href="">click here</a> 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -