1

I am using HttpContext.Request.IsAjaxRequest() to check if an incoming request is from an ajax call.

(actually from Valums FileUploader script)

This works correctly for Chrome but not in IE?

What gives? Is there another way to check this accurately across all browsers?

Thanks.

shenku
  • 11,969
  • 12
  • 64
  • 118

4 Answers4

1

IsAjaxRequest() is simply checking for the existence of the "X-Requested-With" header in the HTTP Request. If it is not present, or has the wrong value, then the check will return false.

My guess is that Chrome and FF are probably adding this for you when an AJAX request is made if it isn't already present, but IE is not.

This means there is more than likely a bug in the script, and it is not adding the header. It should be easy enough to check this out using Fiddler, or the IE developer tools.

To fix this you have one of two options.

  1. Fix the script so that it adds the header to the XHR request.
  2. Use a different script that doesn't have the bug.
Josh
  • 44,706
  • 7
  • 102
  • 124
  • I am using the jquery post method I would have thought it would do this automagically? – shenku Sep 03 '12 at 01:44
  • Depends on which version of jQuery you are using? – Josh Sep 03 '12 at 01:48
  • @Josh: jQuery has set that header as far back as v1.0. That's not the problem. – Dave Ward Sep 03 '12 at 02:06
  • I am using 1.7.2, checked with fiddler, with chrome it adds the x-requested-with header correctly, but with IE not. Interesting. – shenku Sep 03 '12 at 02:18
  • @DaveWard - But people have been filing bugs against it as late as 1.6.2 --- So it was still a possibility if the version of jQuery was old enough. – Josh Sep 03 '12 at 02:20
  • @Josh: jQuery has added that header since 1.0, circa 2006. I'm pretty sure he isn't using a version older than that. – Dave Ward Sep 03 '12 at 02:25
  • @DaveWard - I realize that it has been doing that, but people have reported bugs as far recent as 1.6.2 a quick search of stack overflow will show several questions where people were experiencing issues where the header was not added under certain circumstances. It's clear that isn't what is going on here, but to say this has been rock solid since 1.0 denies experience http://stackoverflow.com/questions/1885847/jquery-no-x-requested-with-xmlhttprequest-in-ajax-request-header – Josh Sep 03 '12 at 02:30
  • @Josh: The code that adds the header is simple. There's no opportunity for truly intermittent issues with it as long as you're making an XHR request. Those issues (including the one you linked, from a quick read of the thread) always trace back to a situation where jQuery can't use XHR (like JSONP and some multi-part scenarios). – Dave Ward Sep 03 '12 at 04:14
1

If like me you have used the same URL for ajax and a 'normal' page. Then it's possible for IE to use the cached version. If that's the case it will respond with a 302 response (I think).

The fix

 $.ajax({
                    method: "GET",
                    cache:false,

Add the cache flag to your ajax.

Richard Housham
  • 1,525
  • 2
  • 15
  • 31
0

IsAjaxRequest tests for the presence of a certain HTTP header that libraries like jQuery set when you use their AJAX helpers. The uploader script you're using falls back to a hidden iframe in IE. Since jQuery's AJAX helper isn't being used in the iframe fallback, the header MVC looks for isn't being set on the request.

I'm not familiar enough with that uploader library to know if it passes querystring parameters through in its action, but I'd suggest trying an action like /controller/upload/?IsAjaxRequest=true. Then, you could check Request.QueryString["IsAjaxRequest"] == "true" and it should work regardless of client-side implementation (assuming the uploader does pass that querystring through).

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
0

It is actually a Jquery 1.7.2 bug because it doesn't add "X-Requested-With" header. They fixed it in version 1.8.2. PS: I tested this myself