0

我在 Laravel 5.5 中更新数据时遇到问题。当我尝试更新资源控制器的记录时,我得到了MethodNotAllowedException,但我正在使用PUT方法。以下是详细信息:

路线:

Route::middleware(['auth'])->group(function(){
    Route::get('/', 'DashboardController@index')->name('home')

    Route::resource('stages', 'StagesController');

});

形式: 在此处输入图像描述

阿贾克斯方法:

$('#ajax_form_modal').on('submit', 'form', function (event) {
    event.preventDefault();
    var $modal = $('#ajax_form_modal');
    var $modal_dialog = $modal.find('.modal-dialog');
    var $form = $(this);
    var method = 'post';

    if ($form.find('[name="_method"]')) {
        method = $form.find('[name="_method"]').val(); //this fires
    } else if ($form.has('method')) {
        method = $form.attr('method');
    }

    //method = 'PUT'

    $.ajax({
        url: $(this).attr('href'),
        dataType: 'json',
        type: method,
        data: $(this).serialize(),

回复:

"trace": [
    {
        "file": "\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\RouteCollection.php",
        "line": 238,
        "function": "methodNotAllowed",
        "class": "Illuminate\\Routing\\RouteCollection",
        "type": "->",
        "args": [
            [
                "GET",
                "HEAD",
                "POST"
            ]
        ]
    },

路线:列表: 在此处输入图像描述

我不知道,为什么更新方法只允许'GET','HEAD','POST',我尝试使用 Post 方法手动处理更新并且它有效,还尝试调试发生路由不匹配但没有运气的地方。谁能给点建议?我真的不想破坏我的控制器的 RESTFUL。

4

1 回答 1

0

我发现了错误:href 应该是行动,需要更多的睡眠......

$.ajax({
    url: $(this).attr('href'),
    dataType: 'json',
    type: method,
    data: $(this).serialize(),
于 2017-09-13T09:15:18.187 回答