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

127
Views
Empty request data from ajax in laravel 5.4

I want to change a value based on the selected year and week with ajax.

This is my ajax function:

function changeBekeken() {
        var year = $("#jaar-nr").val();
        var week = $("#week-nr").val();
        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            url: "/planning/viewed",
            type: "POST",
            data: {
                year_nr: year,
                week_nr: week
            },
            dataType: "json",
            success: function(data) {
                alert(data);
            }
        });
    }
    $(document).ready(function(){
        $("#week-nr").change(function(){
            changeBekeken();
        });
        $("#jaar-nr").change(function(){
            changeBekeken();
        });
    });

This is the route:

Route::post('/planning/viewed', 'PlanningController@viewed');

The data is send to a controller function:

public function viewed(Request $request) {
    $check = DB::table('planning_files')
        ->where([
            ['user_id', Auth::user()->id],
            ['created_year', $request->input('year_nr')],
            ['week', $request->input('week_nr')],
        ])
        ->select('seen')
        ->first();
    if($check) {
        return json_encode($check, JSON_PRETTY_PRINT);
    } else {
        return json_encode(false);
    }
}

However even when I use $request->all(); request is always empty. My function will always return false. Pretty much this exact same function works on another page, but I can't seem to get this one working.

I have tried adding some extra information to the ajax function, like content type and data type, but none seem to work.

Any tips?

Edit 1

These are the namespaces and classes I use in my controller:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Auth;
use Storage;

Edit 2

When I copy the function to another page it works properly. It has something to do with the content on the page I use the ajax function on. Probably some conflict with the token. I will try to resolve this and post the answer for others if they might have the same problem in the future.

over 4 years ago · Santiago Trujillo
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!