【问题标题】:Laravel 5.5 MethodNotAllowed on update using PUT methodLaravel 5.5 MethodNotAllowed 使用 PUT 方法更新
【发布时间】:2017-09-12 18:48:08
【问题描述】:

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

路线:

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

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

});

形式:

Ajax 方法:

$('#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。

【问题讨论】:

    标签: rest laravel-routing php-7.1 laravel-5.5


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-07-01
      • 1970-01-01
      • 2018-07-06
      • 2018-06-12
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2021-11-16
      相关资源
      最近更新 更多