Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to know if the request is ajax in asp.net mvc?

anybody how can I know if the request is ajax ? (I'm using jquery for ajax)

like image 291
Omu Avatar asked Oct 05 '10 13:10

Omu


People also ask

How do I know if request is AJAX request?

In Laravel, we can use $request->ajax() method to check request is ajax or not.

How do I know if AJAX request is successful?

$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });

What is the difference between AJAX request and HTTP request?

An AJAX request is just an HTTP request made by JavaScript code. Browsers load web pages by sending requests to the server using HTTP. Each time a page loads, a request is made. JavaScript code can do the same thing, but without needing to reload the whole page.

Does MVC use AJAX?

The MVC Framework contains built-in support for unobtrusive Ajax. You can use the helper methods to define your Ajax features without adding a code throughout all the views. This feature in MVC is based on the jQuery features.


2 Answers

There's also the Request.IsAjaxRequest if you're using a later version of MVC. I don't have version 1 anymore so I can't say if it's in version 1.

If you need this check in Global.asax.cs try this: new HttpRequestWrapper(Request).IsAjaxRequest()

like image 172
Buildstarted Avatar answered Sep 26 '22 00:09

Buildstarted


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.

like image 35
D'Arcy Rittich Avatar answered Sep 26 '22 00:09

D'Arcy Rittich