不明白为什么要在方法中出错。我做错了什么?
 我正在使用js的Ziggy路由

management.site.destroy:
domain: null
methods: ["DELETE"]
uri: "management/site/{id}"


出现控制台错误
DELETE http://localhost/blog/public/management/site 405 (Method Not Allowed)
上面有按钮和js

<button type="button" name="ok_button" id="ok_button" class="btn btn-danger">OK</button>


JS

$(document).on('click', '#ok_button', (function (e) {
    var product_id = $(this).val();
    var token = $("meta[name='csrf-token']").attr("content");
    $.ajax({
        url: route('management.site.destroy',product_id),
        beforeSend:function(){
            $('#ok_button').text('Deleting...');
        },
        type: 'delete',
        data: {'product_id':product_id,
            '_token': token,},
        success: function (data) {
                setTimeout(function(){
                    $('#confirmModal').modal('hide');
                    alert('Data Deleted');
                    location.reload();
                }, 2000);
        }
    });
}));


控制器:

    public function destroy($id)
    {

        $company_id = Auth::user()->company_id;
    $item = Site::firstWhere(['company_id'=>$company_id,'id'=>$id]);
    $item->delete();
    return response()->json(['success' => 'Data is successfully Deleted']);
    }


补丁中的路由(已编辑,添加了完整路由)等工作正常

Route::group([ 'as'=>'management.','namespace' => 'Management', 'prefix' => 'management','middleware' => ['role:administrator'] ], function () {
    Route::get('/', 'ManagementController@index');
    Route::group(['as' => 'site.','prefix' => 'site'], function () {
        Route::get('/','SiteController@index')->name('index');
        Route::post('store','SiteController@store')->name('store');
        Route::post('edit/{id}','SiteController@edit')->name('edit');
        Route::get('edit/{id}','SiteController@edit')->name('edit');
        Route::patch('','SiteController@update')->name('update');
        Route::delete('{id}','SiteController@destroy')->name('destroy');
        Route::get('{id}','SiteController@view')->name('view');
    });

最佳答案

这是:

Route::delete('{id}','SiteController@destroy')


包裹在Route组中?

如果不是,那么您的delete()方法路由实际上将是/{id}而不是management/site/{id}



在控制台中,运行php artisan route:list以显示您的应用程序的已注册路由的完整列表。然后检查删除方法所注册的路由是什么。



编辑(第2轮)

因此,注册的路线为:

| DELETE | management/site/{id} | management.site.destroy | App\Http\Controllers\Management\SiteController@destroy | web,role:administrator


这期望删除请求为http://localhost/management/site/{id}

但是,返回的错误表明请求的路径不正确:

DELETE http://localhost/blog/public/management/site 405 (Method Not Allowed)


您的某处可能有一个相对路径,该路径正在添加URI的/blog/public/部分!

TLDR;

http://localhost/blog/public/management/site!= http://localhost/management/site/{id}

关于javascript - 在laravel 6中,Ajax Delete给出405(不允许的方法),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61634054/

10-09 22:42