我使用laravel控制器创建了一个宁静的API 。我有一个PhotosController具有destroy($id)删除资源的方法。我也有一段JavaScript代码向DELETE我的应用发送请求。结果应该是删除带有$idID 的照片。但是laravel不会将我的请求路由到destroy方法。而是发送401未经授权错误。
问题是我想通过发送DELETE请求到我的应用程序Ajax,但是laravel不允许我的请求被路由!
route.php文件:
Route::resource('photos', 'PhotosController');
Run Code Online (Sandbox Code Playgroud)
销毁方法:
public function destroy($id)
{
try{
unlink($_SERVER["DOCUMENT_ROOT"].'/uploads/doctors/' . $id);
Session::forget('photo');
$msg = Notification::where('flag', 's')->where('code', 'user-update-delete-photo-gallery')->first()->msg;
return Response::json(array('success' => $msg));
}catch (Exception $e){
App::abort(500, $e->getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
我的Ajax请求:
$.ajax(
{
url: "/photos/" + name,
method : "DELETE", // Or POST : result is the same
data :{
_token : $("input[name=_token]").val(),
_method : …Run Code Online (Sandbox Code Playgroud)