Missing required parameters for [Route: sender] [URI: {name}/{code}] [Missing parameters: name, code]

This may seem duplicate but I have this problem using AJAX and I don’t know how to fix it.

Routes: web.php

Route::get('/{name}/{code}', '[email protected]');
Route::post('/{name}/{code}', '[email protected]')->name('sender');

Controller: TestController.php

public function counter() {
    return view('room');
}

public function sender(Request $request) {
    $xd = array(
        '1' => request('text'), 
        '2' => Request::url()
    );
    event(new TestEvent($xd));
}

View: room.blade.php:

<!doctype html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <div>
            <form>
                @csrf
                <label for="lname">Code:</label><br>
                <input type="text" id="text" name="text"><br><br>
                <input type="submit" value="Submit">
            </form>
        </div>
        <script type="text/javascript">
            $("form").on("submit", function (e) {
                var data = $(this).serialize();
                $.ajax({
                    type: "POST",
                    URL: "{{ route('sender') }}",
                    data: data,
                    success: function () {
                        console.log("works");
                    }
                });
                e.preventDefault();
            });
        </script>
    </body>
</html>

The error is the AJAX if I take it out I don’t get an error. The AJAX code is to call the Controller without redirecting to another page. Any idea how to fix it?

Answer

Use This Route:

Routes: web.php

Route::get('', '[email protected]');
Route::post('', '[email protected]')->name('sender');

If you want to send a parameter, then define your parameter in your route, like below:

Routes: web.php

Route::get('/{name}/{code}', '[email protected]');
Route::post('/{name}/{code}', '[email protected]')->name('sender');

Controller: TestController.php

public function counter($name, $code) {
    //---Note: Use $name and $code in your method
    return view('room', compact('name', $code));
}

public function sender(Request $request, $name, $code) {
    //---Note: Use Those parameters in your method
    $xd = array(
        '1' => request('text'), 
        '2' => Request::url()
    );
    event(new TestEvent($xd));
}

View: room.blade.php:

<!doctype html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <div>
            <form>
                @csrf
                <label for="lname">Code:</label><br>
                <input type="text" id="text" name="text">
                <br />
                <br />
                <input type="submit" value="Submit">
            </form>
        </div>
        <script type="text/javascript">
            $("form").on("submit", function (e) {
                var data = $(this).serialize();
                $.ajax({
                    type: "POST",
                    URL: "{{ route('sender', ['name' => '', 'code' => '']) }}",
                    data: data,
                    success: function () {
                        console.log("works");
                    }
                });
                e.preventDefault();
            });
        </script>
    </body>
</html>