日期:2014-05-16  浏览次数:20707 次

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再试,妥妥的。

?