Django is_ajax history back

♀尐吖头ヾ 提交于 2019-12-13 17:06:42

问题


I wrote a Django view that responses ether a text/html or a application/json depending on request.is_ajax(). So far so good, but when I use my browsers history buttons, I end up getting a JSON response rather than the HTML.

I can't figure out the problem. It's true an jQuery ajax request is getting the same url after the page was loaded, but that shouldn't end up in the history, or should it?

Thanks, Joe


回答1:


If you send different content depending on request.is_ajax(), you need to send Vary: X-Requested-With to the browser. That way, the browser will be able to distinguish the two kinds of response based on the value of the X-Requested-With header on the request. You can do that via:

from django.views.decorators.vary import vary_on_headers

@vary_on_headers('X-Requested-With')
def yourview(request, ...):
    pass


来源:https://stackoverflow.com/questions/20640225/django-is-ajax-history-back

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