SOME TIMES there is no X-Requested-With header, sometimes there is.
I checked in firebug and found that, don\'t know why.
So when I use requ
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");
});
Provide more information. What kind of ajax requests are you making?
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.
Check this post with a similar problem and my answer to it.
X-Requested-With header not set in jquery ajaxForm plugin
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
from jQuery Source
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
Try this also by including the X-Requested-With as part of the Post values.
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!
I apologize I don't know what I was thinking. I must have misread the question when I posted.
This question is for Python django framework. Not for ASP.NET MVC.
I posted this answer because of the ASP.NET MVC's behavior based on the following source code.
Look at the AjaxRequestExtensions.cs class in the ASP.NET MVC source. 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"));
}
}
HttpRequestBase.Item Property
When overridden in a derived class, gets the specified object from the Cookies, Form, QueryString, or ServerVariables collections.
Therefore, request["X-Requested-With"]
will look for that key in all the following places:
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.
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:
$.get('/example/url')
use:
$.get('example/url/')
Looks like it's been a while since you posted this but maybe it will help!
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.)
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:
$('a.basket-add').click(function() { /* etc */ });
To:
$(document).on('click', '.basket-add', function() { /* etc */ });
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 really attached! 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.
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/