I have this following view which I get data from a model and thereafter serialize it into JSON.
views.py
def polling(request):
if request.is_ajax():
data = UserReview.objects.filter(movie_id=request.GET['m_id'])
serializers.serialize('json', data)
return HttpResponse(data, mimetype='application/json')
else:
raise Http404
At the client side I want to show this content now. I’m using jQuery’s function getJSON to archive this. It won’t show anything, and the setTimeout doesn’t work as well. But I get a response when I debug it with firebug, it however doesn’t call the alert() function to view the data. I’ve been trying to figure out what the problem could be for some time now. So I wonder if there’s something wrong with my script?
javascript
function polling() {
$.getJSON( "/polling/",
{m_id: {{movie_info.id}} },
function(data) {
alert(data)
setTimeout(polling, 5000)
});
};
Some general methods that will help you find out what is wrong.
Use
console.logvery liberally on the front end to make sure everything is going as plannedhttp://api.jquery.com/jQuery.ajax/ Callback functions as suggested in a comment, make sure you at least logg an error
https://docs.djangoproject.com/en/dev/topics/logging/ set up a debug logger, make sure that you can see what is going on and what django is actually returning as json.
http://docs.python.org/library/pdb.html Better yet drop this badboy anywhere in your code and MAKE SURE that everything is going the right way. If your success is not being called ont the front end i bet the error is in django! find out where.
You can view the errors in the HTML tab in firebug if
debug=Trueor you can just request/polling/through your browser and view the django error screen.using some or any of these should put you in a fine place to solve your problem django dev server makes it an absolute ease to breeze through these errors please do some research and find out the many many debug tools made available to you!