jQuery .load() not working in Django

流过昼夜 提交于 2019-12-08 13:50:37

问题


I'm trying to make a call to a url in Django and load the contents of it. Right now I have:

<script>
    $('.myClass').load('{% url update_dropdown %}',
        {'kind': "Book" },
        function(data){
            alert(data);
     });

</script>

And then the view that update_dropdown refers to is:

@csrf_exempt                              
def update_dropdown(request):
category = request.POST.get('kind', None)
all =False;

args = { 
    "label":category,
    "all":all
        }

return render_to_response('template.html',(args))

However, the .load() won't work for some reason. If I go directly to the URL it shows the data I expect, but .load() won't cooperate. I know it's not a display issue, as the alert will not work (unless I remove the @csrf_exempt, then it alerts the HTML of the error page)

I'm pretty confused as to what's going on, and I've been debugging this and trying to find the error for hours now, any help would be appreciated .

I can get it to work if I make the return type a JSON object and use getJSON(), but I'd prefer not to


回答1:


Try wrapping it in a ready:

$(document).ready( function () {
    $('.myClass').load('{% url update_dropdown %}',
        {'kind': "Book" },
        function(data){
            alert(data);
     });    
});



回答2:


Apparently it was an issue with the jQuery uiSelect library I was using. It was out of date and causing errors.



来源:https://stackoverflow.com/questions/6643242/jquery-load-not-working-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!