Ajax call Laravel Route
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Properly Calling Laravel Routes or Controllers Using Ajax
Ajax is an essential tool for modern web applications that require dynamic data loading without reloading the entire page. In this blog post, we will discuss how to call Laravel routes and controllers efficiently using Ajax with detailed examples. To avoid common issues, it's crucial to understand the proper way of making Ajax calls in a Laravel application.
Understanding Route Configuration
To make Ajax calls successful, you must first ensure that your routes are properly configured. In your project, routes should be defined under the "app/Http/routes.php" file. Here is an example of a route group for managing product-related tasks:# Middleware group if user is successfully logged in
Route::group(['middleware' => 'auth'], function ()
{
Route::get('/home', ['as' => 'home', 'uses' => 'PageController@showHome']);
# Product group
Route::group(['prefix' => 'product'], function()
{
Route::get('/', ['as' => 'indexProduct', 'uses' => 'ProductController@indexProduct']);
Route::get('new', ['as' => 'newProduct', 'uses' => 'ProductController@newProduct']);
Route::get('show/{productID}', ['as' => 'showProduct', 'uses' => 'ProductController@showProduct']);
Route::get('edit/{productID}', ['as' => 'editProduct', 'uses' => 'ProductController@editProduct']);
Route::post('create', ['as' => 'createProduct', 'uses' => 'ProductController@createProduct']);
Route::post('update', ['as' => 'updateProduct', 'uses' => 'ProductController@updateProduct']);
Route::delete('destroy', ['as' => 'destroyProduct', 'uses' => 'ProductController@destroyProduct']);
});
});
Using Laravel Controller and Routes in Ajax Calls
When making an Ajax call to a Laravel route or controller, make sure the routes are configured properly. For example, you can generate a new product by sending data through Ajax:<?php
namespace App\Http\Controllers;
use Input;
use App\Product;
use App\Companies;
use App\Http\Controllers\Controller;
class ProductController extends Controller
{
public function createProduct()
{
$data = Input::all();
$product = new Product;
$product->fill($data);
if($product->save())
{
return redirect()->route('root')->with('message','Success');;
}
}
}
Your Ajax call should look something like this:
$("#input-logo").fileinput({
uploadUrl: '{{route("product/create")}}',
type: 'POST',
allowedFileExtensions: ["jpg", "png", "gif", "jpeg"],
allowedFileTypes: ['image'],
headers: {
'X-CSRF-Token': $('#_token').val(),
}
}).on('filepreupload', function() {
$('#kv-success-box').html('');
}).on('fileuploaded', function(event, data) {
$('#kv-success-box').append(data.response.link);
$('#kv-success-modal').modal('show');
});