how can I detect if a request is an AJAX-request (from jQuery) on the server-side of a ASP.net application. I don´t want to do this: mypage.aspx?this_is_ajax=true…
ex.
$.get("mypage.aspx");
On the server side I want to do certain things when the request is a ajax request…
Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
ASP.NET MVC had a great extension for this, that did work with JQuery. It checked it this way:
Check the core collection:
request["X-Requested-With"] == "XMLHttpRequest"
Check the headers collection (ensuring its not null):
request.Headers["X-Requested-With"] == "XMLHttpRequest"
It did it as an extension method, so you could recreate it, or use it if you are using ASP.NET MVC through Request.IsAjaxRequest().
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0