[英]Ajax Delete give 405 (Method Not Allowed) in laravel 6
不明白為什么必須在方法中出錯。 我做錯了什么? 我正在為 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);
}
});
}));
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::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}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.