1

why following jquery code doesn't work when used with django while it works if loaded like static page?

there's probably django's csrf protection involved somewhere, but i can't find how to make it work.

edited, stripped down code:


$(document).ready(function(){
$('.content').load('something.txt');
    $.ajax({
    method: "get",url: "http://something.com/pm/js/something.txt",
    success: function(html)
            {
            $(".content").html(html);
            }
    });
});

the purpose of this should be: when this django page loads, the script should call another django view(s) and load data from them. (at this moment to make it easier 'something.txt' is static file)

...firebug doesn't show anything abnormal

3
  • here's how I would debug that: 1.take out the click handler. 2.add an error function parameter inside of $.ajax. 3.inspect the call w/ firebug or charles. You don't say which of the ajax calls work and which ones don't. Also, you don't say if there is any dynamic data being loaded that may affect execution. Commented Feb 24, 2011 at 2:45
  • I've stripped it down to make it even more simple, but the results are just the same: it works without any problems in the static pages served by nginx and when I do the same using django it ignores the javascript. I suspect that there's involved django's Cross Site Request Forgery protection, but I haven't found any informations about this csrf/django/ajax thing Commented Mar 2, 2011 at 7:20
  • CSRF has nothing to do with this because it's a "get" request instead of a "post" request. Commented Jan 24, 2014 at 19:43

1 Answer 1

0

I'm going to take a wild guess: maybe when you load the page with Django you don't have static file serving setup properly, so jQuery never gets loaded, and as such none of your Javascript gets executed.

To check and see if jQuery is getting loaded open firebug on that page and see if the jQuery variable is undefined.

If that's the problem just load jQuery from a CDN for now.

<script src="http://code.jquery.com/jquery-1.5.1.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.