简体   繁体   English

Ajax 删除 laravel 6 中的 405(不允许的方法)

[英]Ajax Delete give 405 (Method Not Allowed) in laravel 6

Don't understand why have to error in the method.不明白为什么必须在方法中出错。 What I do wrong?我做错了什么? I'm using Ziggy routing for js我正在为 js 使用 Ziggy 路由

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

Have console error DELETE http://localhost/blog/public/management/site 405 (Method Not Allowed) have button and js on it有控制台错误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 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);
        }
    });
}));

Controller: Controller:

    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 (Edited added full route) in patch and etc work fine补丁中的路线(编辑添加的完整路线)等工作正常

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

Is this:这是:

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

wrapped in a Route group?包裹在Route组中?

If it is not, then your delete() methods route will actually be /{id} and not management/site/{id}如果不是,那么您的delete()方法路线实际上将是/{id}而不是management/site/{id}


In your console, run php artisan route:list to display the full list of registered routes for your application.在您的控制台中,运行php artisan route:list以显示您的应用程序的已注册路由的完整列表。 Then check what the registered route is for your delete method.然后检查您的删除方法的注册路由是什么。


Edit (Round 2)编辑(第 2 轮)

So the registered route is:所以注册的路线是:

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

This is expecting the delete request to be http://localhost/management/site/{id}这期望删除请求是http://localhost/management/site/{id}

However, the error being returned indicates the path the request is making is incorrect:但是,返回的错误表明请求发出的路径不正确:

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

It may well be that you have a relative path somewhere which is adding the /blog/public/ section of your URI!很可能您在某处有一个相对路径,它正在添加您的 URI 的/blog/public/部分!

TLDR; TLDR;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM