django在反向代理的环境中使用reuqest.is_ajax()

django1.3的request object文档中写道
HttpRequest.is_ajax()
Returns True if the request was made via an XMLHttpRequest, by checking the HTTP_X_REQUESTED_WITH header for the string 'XMLHttpRequest'. Most modern JavaScript libraries send this header. If you write your own XMLHttpRequest call (on the browser side), you'll have to set this header manually if you want is_ajax() to work.

 开始不清楚头部的命名规范,发送异步请求的时候添加了HTTP_X_REQUESTED_WITH头,测试不行。再看了下django文档

django文档 写道
With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.

 

原来HTTP是自动添加的前缀,于是改成X_Requested_With,还是不行。后来想了下django服务是用nginx反向代理的,非标准的头是要自行设定的。于是打开nginx配置文件,再server里添加了:

 fastcgi_pass_header X_Requested_With;

以为可以了,一试还是不行,头晕了。google了下发现nginx是不认下划线"_"的,将头改为:

X-Requested-With

nginx配置文件改为:

fastcgi_pass_header X-Requested-With;

重启nginx再试,妥妥的。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值