简体   繁体   中英

How to get to a laravel route using jquery ajax

I am trying to put to a method in a controller using ajax but I keep getting a 404 not found.

My JS:

var dataroomForm = $('#dataroomForm');
var dataroomId = $('#dataroomId').val();
var saveButton = $('.saveDataroom');

saveButton.click(function(e) {
    var dataroomData = dataroomForm.serialize();
    e.preventDefault();
    console.log(dataroomData);

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $.ajax({

        url: '/dataroom/' + dataroomId,
        type: 'PUT',
        data: {
            'dataroom': dataroomData
        },
        dataType: 'json',
        success: function (data) {
            console.log(data);
        },
        error: function (request, error) {
            alert("Request: " + JSON.stringify(request));
        }
    });

});

The route I am trying to trigger:

Route::put('dataroom/{dataroom}', ['as' => 'update', 'uses' => 'DataroomController@update']);

And update method (not really relevant I think but posting for complete view)

public function update(DataroomRequest $request, $id, MailService $mailService)
{
    $dataroom = Dataroom::withoutGlobalScope(ActiveScope::class)->find($id);
    if (is_null($dataroom)) {
        \Flash::error('Dataroom niet gevonden!');
        return redirect(route('admin.dataroom.index'));
    }

    $dataroom->fill($request->all());
    $dataroom->save();

    $dataroom->handleUploader($request, 'image');

    if ($request->has('send_update')) {
        $changes = $request->input('changes');

        foreach ($dataroom->accounts as $account) {
            $mailService->sendUpdate($account, $dataroom, $changes);
        }

        if (empty($changes)) {
            $changes = 'Dataroom gewijzigd';
        }

        $dataroom->log($changes);
    }

    \Flash::success('De wijzigingen voor <strong>' . $dataroom->title . '</strong> zijn opgeslagen');

    return redirect()->route('admin.dataroom.index');
}

In my.network tab I get:

Request URL: http://website.nl.test/dataroom/26
Request Method: PUT
Status Code: 404 Not Found

And as response: exception: "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException"

What am I doing wrong?

I don't see any issues in your code try using the command in your terminal php artisan optimize:clear in root folder this will flush/remove the application cache, route cache, and config cache altogether.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM