anybody how can I know if the request is ajax ? (I'm using jquery for ajax)
In Laravel, we can use $request->ajax() method to check request is ajax or not.
$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });
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.
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.
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With