find a location for property in a new city

Friday 1 October 2010

ASP.NET MVC IsAjaxRequest() not recognising request from jQuery $.ajax()

If you are finding that an AJAX request from, say, a piece of jQuery AJAX code such as $.ajax() $.get() or $.post() or you own custom AJAX code (you legend) is not registering as an AJAX request when using the IsAjaxRequest() then the problem is probably to do with X-Requested-With.

I used .NET Reflector to have a look at how IsAjaxRequest() works and it is something like this:
return ((request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest")));

So that means you need to have a header of "X-Requested-With" set to "XMLHttpRequest" which is usually the case when you are using AJAX libraries like ASP.NET's or jQuery's. You can see if this is the case using Fiddler.

However, interestingly you can also set this as either part of your form POST or even a GET querystring! Such as www.example.com?x-requested-with=XMLHttpRequest (case sensitive). This will also make IsAjaxRequest() return true.

Follow britishdev on Twitter

10 comments:

  1. Try also data room software and you will see that it helps you to save time and money!

    ReplyDelete
  2. Now I am ready to do my breakfast, when having my breakfast coming again to read further news.
    Top seo blog

    ReplyDelete
  3. I like your blog i read this blog so i am impressed and please next update your content i wait for next update this blog .and i daily follow this site
    Best vpn

    ReplyDelete
  4. I am curious to find out what blog system you’re using?

    ----------------------------------
    Best web development software Company in Kanpur

    ReplyDelete
  5. really the information is in article is very awesome...
    Regards
    Software development company in kanpur

    ReplyDelete
  6. This is extremely useful post and enlightening There is no uncertainty about it. I discovered this one really decent and it ought to go into my accumulation. Visit Websites It helped me from numerous points of view.

    ReplyDelete
  7. Excellent read, Positive site, where did u come up with the information on this posting? I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work,If anyone interested similar one's have a look here thanks.

    shih tzu puppies for sale near me
    buy puppy online
    fuhrerschein-kaufen-schweiz

    ReplyDelete