Jquery,ajax 请求标头中没有 X-Requested-With=XMLHttpRequest?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1885847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery, No X-Requested-With=XMLHttpRequest in ajax request header?
提问by Dong
SOME TIMESthere is no X-Requested-With header, sometimes there is.
有些时候,不存在X-请求-随着头,有时有。
I checked in firebug and found that, don't know why.
我检查了萤火虫,发现了,不知道为什么。
So when I use request.is_ajax in django, it fails sometimes.
所以当我在 Django 中使用 request.is_ajax 时,它有时会失败。
Anyone know how to fix it?
谁知道怎么修它?
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:
好吧,现在又发生了。我打开页面,然后离开吃晚饭很久,当我回来时,它又发生了。我在 firbugs 中记录了请求头:
Request with X-Requested-with:
请求与 X-Requested-with:
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
Refererhttp://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
Refererhttp://localhost:8000/gallery/
Cookie xxx
Request without X-Requested-with:
不带 X-Requested-with 的请求:
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
Refererhttp://localhost:8000/gallery/
Cookie xxx
主机 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
Refererhttp://localhost:8000/gallery/
Cookie xxx
回答by jitter
Provide more information. What kind of ajax requests are you making?
提供更多信息。你在做什么类型的ajax请求?
If you are submitting forms which contain an input field of type file that is most likely the reason that the header is missing.
如果您提交的表单包含文件类型的输入字段,这很可能是标题丢失的原因。
As you can't submit a file with ajax, all the javascript frameworks use the "hidden iframe" trick internally to get the work done for you.
由于您无法使用 ajax 提交文件,因此所有 javascript 框架都在内部使用“隐藏的 iframe”技巧来为您完成工作。
Check this post with a similar problem and my answer to it.
用类似的问题检查这篇文章和我对它的回答。
X-Requested-With header not set in jquery ajaxForm plugin
未在 jquery ajaxForm 插件中设置 X-Requested-With 标头
Otherwise there should be no reason for such a behavior from jQuery as it always sets the header. If the issue isn't related to file-inputs please post relevant codesnippets
否则 jQuery 的这种行为应该没有理由,因为它总是设置标题。如果问题与文件输入无关,请发布相关代码片段
from jQuery Source
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
来自 jQuery 源
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
回答by Levitikon
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.
我在 1.6.2 版本中也遇到了这个问题,尤其是当页面已经设置空闲一段时间时。扩展抖动的答案,这是在一个地方为每个请求冗余添加 X-Requested-With。我把它放在我的母版页。希望这能奏效。
$(document).ajaxSend(function (event, request, settings) {
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
});
回答by yuikonnu
The reason I got this was because I was taking an element with my AJAX event handler attached, cloning it and then adding the new element to my document. For some reason this meant that the event attached to the new element would not treat it as a normal ajax request (Accept:text/html instead of application/json, no X-Requested-With etc.)
我得到这个的原因是因为我正在获取一个附加了我的 AJAX 事件处理程序的元素,克隆它,然后将新元素添加到我的文档中。出于某种原因,这意味着附加到新元素的事件不会将其视为普通的 ajax 请求(Accept:text/html 而不是 application/json,没有 X-Requested-With 等)
To fix this all I did was change the event to jQuery's new equivalent of live()
(unsure what they call it now). So from:
为了解决这个问题,我所做的就是将事件更改为 jQuery 的新等价物live()
(不确定他们现在叫什么)。所以从:
$('a.basket-add').click(function() { /* etc */ });
To:
到:
$(document).on('click', '.basket-add', function() { /* etc */ });
回答by David
this may not be THE answer, but I was clawing my eyeballs out for a couple hours before figuring out that my mistake was pretty bone-headed. (What was killing me is that it was working on my dev box but not in production, and I still don't get why that is so, but...)
这可能不是答案,但我花了几个小时才发现我的错误非常愚蠢。(让我丧命的是它在我的开发箱上工作但没有在生产中工作,我仍然不明白为什么会这样,但是......)
Make sure the event handler is reallyattached! If there's no X-Requested-With
header it may be for good reason! I had a form whose action attribute was the correct url, and my submit button's "click" event was saying event.preventDefault()
and then calling $.post(...)
, yadda yadda. Problem is, that DOM element was getting replaced as the result of some other xhr activity, and its event handler was getting blown away with it. The form was being submitted as a plain old POST, not ajax, hence no header.
确保事件处理程序真的附加了!如果没有X-Requested-With
标题,那可能是有充分理由的!我有一个表单,其 action 属性是正确的 url,我的提交按钮的“点击”事件在说event.preventDefault()
然后调用$.post(...)
yadda yadda。问题是,由于其他一些 xhr 活动,该 DOM 元素正在被替换,并且它的事件处理程序被它吹走了。表单作为普通的旧 POST 提交,而不是 ajax,因此没有标题。
So, rather than
所以,而不是
$('#my-submit-button').on("click", data, function(event) {
event.preventDefault();
$.post(/* etc */);
})
it needed to be something like
它需要像
$('#parent-div').on("click","#my-submit-button", function(event){
event.preventDefault();
$.post(/* etc */);
});
Further reading: https://learn.jquery.com/events/event-delegation/
回答by stun
Try this also by including the X-Requested-Withas part of the Post values.
也可以尝试将X-Requested-With作为 Post 值的一部分。
var postData = "X-Requested-With=XMLHttpRequest&" + $("#myFormId").serialize();
$.post(
'http://www.mysite.com/blahblah',
postData,
function(data) { /*do whatever*/ },
'html'
);
That and combine it with jitter's answer. Hope it helps!
并将其与jitter's answer结合起来。希望能帮助到你!
EDIT NOTES:
编辑注释:
I apologize I don't know what I was thinking. I must have misread the question when I posted.
This question is for Python djangoframework. Not for ASP.NET MVC.
我很抱歉我不知道我在想什么。我发布时一定误读了这个问题。
这个问题是针对Python django框架的。不适用于 ASP.NET MVC。
I posted this answer because of the ASP.NET MVC's behavior based on the following source code.
由于基于以下源代码的 ASP.NET MVC 行为,我发布了此答案。
ASP.NET MVC Source Code
ASP.NET MVC 源代码
Look at the AjaxRequestExtensions.csclass in the ASP.NET MVC source. http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/60c2f18ed84838b1b3da671536a7a40033e67b0d#src/System.Web.Mvc/AjaxRequestExtensions.cs.
查看ASP.NET MVC 源代码中的AjaxRequestExtensions.cs类。http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/60c2f18ed84838b1b3da671536a7a40033e67b0d#src/System.Web.Mvc/AjaxRequestExtensions.cs。
public static class AjaxRequestExtensions
{
public static bool IsAjaxRequest(this HttpRequestBase request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
return (request["X-Requested-With"] == "XMLHttpRequest") ||
((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
}
}
MSDN Documentation on HttpRequestBase.ItemProperty
关于HttpRequestBase.Item属性的MSDN 文档
HttpRequestBase.Item Property
When overridden in a derived class, gets the specified object from the Cookies, Form, QueryString, or ServerVariablescollections.
HttpRequestBase.Item 属性
在派生类中重写时,从Cookies、Form、QueryString或ServerVariables集合中获取指定的对象。
Therefore, request["X-Requested-With"]
will look for that key in all the following places:
因此,request["X-Requested-With"]
将在以下所有位置查找该密钥:
- HTTP Form POST values
- HTTP Cookie
- HTTP Request Query String
- and Server Variables.
- HTTP 表单 POST 值
- HTTP Cookie
- HTTP 请求查询字符串
- 和服务器变量。
So, if you include the X-Requested-With=XMLHttpRequest
key-value pair as part of the HTTP POST like I am doing in the jQuery AJAX call, ASP.NET MVC will consider the HTTP Request as an AJAX HTTP Request.
因此,如果X-Requested-With=XMLHttpRequest
像我在 jQuery AJAX 调用中那样将键值对作为 HTTP POST 的一部分包含在内,ASP.NET MVC 会将 HTTP 请求视为 AJAX HTTP 请求。
回答by lizlux
I was just having the same problem myself, and I fixed it by ensuring there was a trailing slash after my url. So instead of:
我自己也遇到了同样的问题,我通过确保我的 url 后面有一个斜杠来修复它。所以而不是:
$.get('/example/url')
use:
用:
$.get('example/url/')
Looks like it's been a while since you posted this but maybe it will help!
看起来你发布这个已经有一段时间了,但也许它会有所帮助!