如何使用jquery AJAX 访问laravel路由

nfs0ujit  于 2022-11-26  发布在  jQuery
关注(0)|答案(1)|浏览(101)

我尝试使用 AJAX 在控制器中放置一个方法,但我一直得到一个404未找到。
我的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));
        }
    });

});

我尝试触发的路由:

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

和更新方法(不是真的相关,我认为,但张贴完整的看法)

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');
}

在“网络”选项卡中,我看到:

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

而作为回应:exception: "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException"
我做错了什么?

6l7fqoea

6l7fqoea1#

我没有看到任何问题,在您的代码尝试使用命令,在您的终端php artisan optimize:clear的根文件夹,这将刷新/删除应用程序缓存,路由缓存,和配置缓存在一起。

相关问题