How to identify requesting coming form ajax or not? [Resolved]

Posted by Allemahesh under ASP.NET on 8/13/2013 | Points: 10 | Views : 6554 | Status : [Member] [MVP] | Replies : 2
How server is know that request is cumming form an ajax?
Means If I send a request to a page in my asp.net, then how will server know if the request is ajax request or normal request?
Can any one tell me?




Responses

Posted by: Bandi on: 8/13/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
All AJAX calls made by jQuery will have a header added to indicate it is AJAX. The header to check is X-Requested-With, and the value will be XMLHttpRequest when it is an AJAX call.

Note that AJAX requests are normal GETs or POSTs, so unless you (or your AJAX library like jQuery) are adding an additional header in the request, there is no way to know for certain whether it is AJAX or not.

public static bool IsAjaxRequest(this HttpRequest request)

{
if (request == null)
{
throw new ArgumentNullException("request");
}

return (request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
}


references:
http://stackoverflow.com/questions/3864179/how-to-know-if-the-request-is-ajax-in-asp-net-mvc
http://stackoverflow.com/questions/4392836/how-to-check-if-request-is-ajax-or-not-in-codebehind-asp-net-webforms

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 8/13/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
Please find the below which has solution for your questions

http://stackoverflow.com/questions/3441735/detect-ajax-call-asp-net
http://stackoverflow.com/questions/3864179/how-to-know-if-the-request-is-ajax-in-asp-net-mvc
http://stackoverflow.com/questions/4392836/how-to-check-if-request-is-ajax-or-not-in-codebehind-asp-net-webforms
http://www.nullskull.com/faq/1891/aspnet--find-if-a-request-is-of-type-ajax.aspx

Regards,
Jayakumar Selvakani

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response