Laravel Route 缺少必需参数

问题描述 投票:0回答:1

我有这个模式可以通过使用 ajax 检索到的数据填充模式来编辑数据。

但是,我遇到了 AJAX 中的 URL 和 Laravel 路由的问题,并出现以下错误:

缺少

[Route: get-project-data] [URI: get-project-data/{projectId}] [Missing parameter: projectId].

所需的参数
<button type="button" class="item edit-button"
    data-toggle="modal" data-id="{{ $item->id }}" data-url="{{ route('get-project-data', ['projectId' => '']) }}"
    data-target="#editModal" data-placement="top"
    title="Edit">
    <i class="zmdi zmdi-edit"></i>
</button>

web.php

Route::get('/get-project-data/{projectId}', [HomeController::class, 'getProjectData'])->name('get-project-data');
$(document).on('click', '.edit-button', function() {
    var projectId = $(this).data('id');
    console.log("Selected Project ID: " + projectId);
    
    // Store projectId in edit modal's data attribute
    $('#editModal').data('projectId', projectId);
    
    // Retrieve the base URL for the AJAX request from the data-url attribute
    var baseUrl = $(this).data('url');
    
    // Construct the complete URL for the AJAX request
    var url = baseUrl + projectId;

    // Fetch existing data of the selected project
    $.ajax({
        url: url,
        method: 'GET',
        /* ... */
    });
});
javascript php ajax laravel
1个回答
0
投票

我添加了

['projectId' => '']

的值
<button type="button" class="item edit-button"
                                                            data-toggle="modal" data-id="{{ $item->id }}" data-url="{{ route('get-project-data', ['projectId' => $item->id ]) }}"
                                                            data-target="#editModal" data-placement="top"
                                                            title="Edit">
                                                            <i class="zmdi zmdi-edit"></i>
                                                        </button>

并在js中使用

data-url

$(document).on('click', '.edit-button', function() {

      var projectId = $(this).data('id');
      console.log(projectId);

      $('#editModal').data('projectId', projectId);
      var baseUrl = $(this).data('url');

      // Fetch existing data of the selected project
      $.ajax({
            url: baseUrl,
            method: 'GET',
            success: function(response) {

© www.soinside.com 2019 - 2024. All rights reserved.