Django is_ajax历史回归 [英] Django is_ajax history back

查看:86
本文介绍了Django is_ajax历史回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Django视图,它响应以下 text / html application / json ,具体取决于 request.is_ajax()
到目前为止还不错,但是当我使用浏览器历史记录按钮时,我最终得到的是JSON响应,而不是HTML。

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.

我无法想象出问题。这是真的一个jQuery ajax请求在页面加载后得到相同的url,但这不应该在历史记录中结束,还是应该呢?

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?

谢谢,Joe

推荐答案

如果根据发送不同的内容,请求.is_ajax() ,您需要向浏览器发送 Vary:X-Requested-With 。这样,浏览器就能够根据请求中 X-Requested-With 标头的值来区分这两种响应。您可以通过以下方式完成此操作:

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

这篇关于Django is_ajax历史回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆