Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I determine on the server side if a given HttpRequest was sent via ajax?

I was wondering what is the best way (if at all possible) to determine on the server side if a given HttpRequest was sent via ajax?

like image 351
Jon Avatar asked Jan 29 '26 04:01

Jon


2 Answers

This may or may not help you out, but you can at least determine if the postback is an asynchronous one (meaning that it was likely sent via ajax). You can do this via the following:

if (sm1.IsInAsyncPostBack)
{
    //code here
}

The sm1 referenced above would be provided for with this:

<asp:ScriptManager id="sm1" runat="server" />
like image 56
alex Avatar answered Jan 31 '26 17:01

alex


An XMLHttpRequest appears just like any other browser request. As far as I know, there's no way to distinguish them, unless the XMLHttpRequest is deliberately setting headers to identify itself as such.

like image 40
Tony Avatar answered Jan 31 '26 16:01

Tony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!