Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery, No X-Requested-With=XMLHttpRequest in ajax request header?

Tags:

jquery

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 request.is_ajax in django, it fails sometimes.

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:

Request 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
Referer http://localhost:8000/gallery/
Cookie xxx

Request without 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
Referer http://localhost:8000/gallery/
Cookie xxx

like image 589
Dong Avatar asked Dec 11 '09 04:12

Dong


People also ask

What is X-requested-with XMLHttpRequest?

The X-Requested-With is a request header that a user agent may use to store information about the creation of the request such as client information, method used. Note that the X-Requested-With cannot be added to a cross domain request without the consent of the server via CORS.

Does Ajax use XMLHttpRequest?

XMLHttpRequest is used heavily in AJAX programming. Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML. If your communication needs to involve receiving event data or message data from a server, consider using server-sent events through the EventSource interface.

Is XMLHttpRequest same as Ajax?

XMLHttpRequest is the raw browser object that jQuery wraps into a more usable and simplified form and cross browser consistent functionality. jQuery. ajax is a general Ajax requester in jQuery that can do any type and content requests.

What does Expected x-requested-with header mean?

1) include an X-Requested-With header that indicates that the request was made by XMLHttpRequest instead of being triggered by clicking a regular hyperlink or form submit button.


2 Answers

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");

like image 173
jitter Avatar answered Nov 15 '22 15:11

jitter


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");
});
like image 23
Levitikon Avatar answered Nov 15 '22 15:11

Levitikon