Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
How to use Ajax in Laravel 8

I tried to use Ajax from laravel, but I couldn't find I want it.

How to use ajax in Laravel?

in main.js file...

$.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url : "{{ route('saveToken') }}",
        data : {'token' : token},
        type : 'POST',
        dataType : 'json',
        success : function(result){

            console.log("===== " + result + " =====");

        }
    });

in blade template ...

<body>
     <meta name="csrf-token" content="{{ csrf_token() }}">
     <div class="wrapper">
     @yield('content')
     <script src="{{ asset('assets/js/main.js') }}"></script>
     </div>                
</body>

php file

//web.php
Route::get('/saveToken', [FcmController::class, 'saveToken']);

// Fcm controller
public function saveToken(Request $request)
{
...

on using ajax url //"{{ route('saveToken') }}"

~~~/%7B%7B%20route('saveToken')%20%7D%7D  404 not found

// '/saveToken'

405 not allowed
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You are calling route name in url so you have to add route name in route .

Route::get('/saveToken', [FcmController::class, 'saveToken']);

Replace with

Route::post('/saveToken', [FcmController::class, 'saveToken'])->name('saveToken');
over 4 years ago · Santiago Trujillo Report

0

Replace your route

Route::post('/saveToken', [FcmController::class, 'saveToken'])->name('saveToken');

OR

Change your ajax

 $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url : "{{ url('saveToken') }}",
        data : {'token' : token},
        type : 'GET',
        dataType : 'json',
        success : function(result){

            console.log("===== " + result + " =====");

        }
    });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!