How to check request is Ajax or not in Laravel 5?
In this post we will give you information about How to check request is Ajax or not in Laravel 5?. Hear we will give you detail about How to check request is Ajax or not in Laravel 5?And how to use it also give you demo for it if it is necessary.
In this post, I will let you know how to determine request is ajax or not in Laravel 5.
Sometime you need to check if request is ajax then response data only and if request is not ajax then response complete view in your application.
By using ajax()
method in Laravel, you can check request is ajax or not.
Laravel Request class has many method to read HTTP request for the current request. You can also check if request is over https or request has json content type.
You can use directly Request facade that grant you access to the current request or you can use instance of Request Class.
Example :
- public functionindex(Request $request)
- {
- if($request->ajax()){
- returnresponse()->json(['status'=>'Ajax request']);
- }
- returnresponse()->json(['status'=>'Http request']);
- }
public function index(Request $request) { if($request->ajax()){ return response()->json(['status'=>'Ajax request']); } return response()->json(['status'=>'Http request']); }
- public functionindex()
- {
- if(Request::ajax()){
- returnresponse()->json(['status'=>'Ajax request']);
- }
- returnresponse()->json(['status'=>'Http request']);
- }
public function index() { if(Request::ajax()){ return response()->json(['status'=>'Ajax request']); } return response()->json(['status'=>'Http request']); }
To determine if the request is over HTTPS :
- if(Request::secure())
- {
- //
- }
if (Request::secure()) { // }
The Request class provides many method to examine HTTP request for your application.
Hope this code and post will helped you for implement How to check request is Ajax or not in Laravel 5?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs