jQuery,ajax请求标头中是否没有X-Requested-With = XMLHttpRequest? [英] Jquery, No X-Requested-With=XMLHttpRequest in ajax request header?

查看:569
本文介绍了jQuery,ajax请求标头中是否没有X-Requested-With = XMLHttpRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些时间,有时没有X-Requested-With标头.

SOME TIMES there is no X-Requested-With header, sometimes there is.

我检查了萤火虫,发现了,不知道为什么.

I checked in firebug and found that, don't know why.

因此,当我在Django中使用request.is_ajax时,有时会失败.

So when I use request.is_ajax in django, it fails sometimes.

有人知道如何解决吗?

好的,现在又发生了. 我打开页面,然后离开晚餐很长一段时间,当我回来时,它又发生了.我在萤火虫中记录了请求标头:

OK, now it happened again. I opened the page and then left for supper for a long while, when I came back, it happened again. I recorded request header in firbugs:

带有X-Requested-with的请求

Request with X-Requested-with:

主机localhost:8000
用户代理Mozilla/5.0(Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.5)Gecko/20091102
Firefox/3.5.5
接受text/html,/
接受语言zh-cn,zh; q = 0.5
接受编码gzip,deflate
Accept-Charset GB2312,utf-8; q = 0.7,*; q = 0.7
保持活动300
连接保持活动
X-Requested-With XMLHttpRequest
推荐人http://localhost:8000/gallery/
Cookie xxx

Host localhost:8000
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.5) Gecko/20091102
Firefox/3.5.5
Accept text/html, /
Accept-Language zh-cn,zh;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset GB2312,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
X-Requested-With XMLHttpRequest
Referer http://localhost:8000/gallery/
Cookie xxx

不带X-Requested-with的请求:

Request without X-Requested-with:

主机localhost:8000
用户代理Mozilla/5.0(Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.5) Gecko/20091102
Firefox/3.5.5 接受text/html,application/xhtml + xml,application/xml; q = 0.9,/; q = 0.8 Accept-Language zh-cn,zh; q = 0.5 Accept-Encoding gzip,deflate
Accept-Charset GB2312,utf-8; q = 0.7,*; q = 0.7
保持活动300
连接保持活动
推荐人http://localhost:8000/gallery/
Cookie xxx

Host localhost:8000
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.5) Gecko/20091102
Firefox/3.5.5 Accept text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language zh-cn,zh;q=0.5 Accept-Encoding gzip,deflate
Accept-Charset GB2312,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
Referer http://localhost:8000/gallery/
Cookie xxx

推荐答案

在1.6.2版中,我也遇到了这个问题,尤其是当页面已闲置一会儿时.扩展抖动的答案,这是在一个地方为每个请求冗余地添加了X-Requested-With.我把它放在我的母版页上.希望能解决这个问题.

I was facing this issue as well with version 1.6.2, especially when the page has been setting idle for a while. Expanding on jitter's answer, this is adding the X-Requested-With redundantly for every request in one place. I put this in my Master page. Hope this works out.

$(document).ajaxSend(function (event, request, settings) {
    request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
});

这篇关于jQuery,ajax请求标头中是否没有X-Requested-With = XMLHttpRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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