Laravel 9 Ajax Post Request Example Tutorial

Hello Artisan

In this tutorial, I am going to discuss laravel and ajax and how to make an ajax post request in Laravel 9. That means now I will discuss how we can use ajax requests in our laravel 9 application to send HTTP requests to the server. 

Ajax request is a fundamental requirement of any PHP project to send the HTTP requests to the server. By using Ajax request we can get or store data into the database without page refresh. Users feel better to use this kind of feature.

In this example, I will show you a simple example of how to create an ajax request in Laravel 9 applications. I will show you how we can send laravel ajax post data to the controller and get data from the controller. We will also see how we can learn about laravel ajax url route with parameters.

I already created many tutorials about laravel and ajax. I already created how to insert data using Ajax requests in laravel 9. You can read it from the below link. 

 

Read also : Insert Data using Ajax Request in Laravel 9

 

But now in this laravel ajax example tutorial we will see how to send Ajax post or get requests in laravel 9. It is pretty simple and easy to use in laravel applications. You have to follow some steps to learn how to use Ajax request in laravel 9.

 Let's start our jquery ajax post request example in Laravel 9 tutorial.

 

Step 1: Create Route

Firstly we have to create two routes. One for view and another for sending HTTP ajax post request to the server. Paste this below code

routes/web.php

Route::get('ajax', 'AjaxController@view');
Route::post('ajax', 'AjaxController@send_http_request')->name('ajaxRequest.post');

 

Step 2: Create Controller

Now we have to create a controller to handle the above two methods. So create a controller and paste the following code.

app/Http/Controllers/AjaxController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
   
class AjaxController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function view()
    {
        return view('test');
    }
   
    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function send_http_request(Request $request)
    {

        \Log::info($request->all());
   
        return response()->json([
            'success'=>'get your data'
        ]);

    }
   
}

 

Since this is our testing tutorial on how to use laravel with ajax requests. So here we don't store our Ajax request data in the database. We see it in the log file.

Read also : Laravel 9 Ajax CRUD Example with Sweet Alert

 

Step 3: Create Blade File

Finally, we have to create our blade component to create an HTML form. So now paste the below content to your test.blade.php file.

resources/views/test.blade.php

 

Now you can run this code and visit those route URLs and give input in Html form then you will get the below output.

storage/logs/laravel.log

[2020-03-27 03:54:19] local.INFO: array (

  'name' => 'Mahedi Hasan',

  'password' => '123456',

  'email' => 'mahedy@example.com',

)  

 

Read also : Laravel 9 Ajax Form Submit With jQuery Validation

 

Hope this laravel ajax post request example tutorial will help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js