Laravel API, how to accept DELETE method

余生颓废 提交于 2019-12-20 06:36:22

问题


I need to use my API to delete some entities, I create my controller, my methods, the routes. They works fine, all the get and put/patch method works, but with the delete one I have and error throw by my Angular app who consume this api, here is the error :

DELETE (Method Not Allowed)

In my api route file I set this :

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods:  POST, GET, OPTIONS, PUT, PATCH, DELETE');
header('Access-Control-Allow-Headers:  Content-Type, X-Auth-Token, Origin, Authorization, X-HTTP-Method-Override');

Anyone know why it's not working ?

Edit:

Here is my routes (and yes the methods are in the good controller) :


回答1:


When you are posting to the Delete url, make sure you have this data in your post request:

_method=delete

this is just like an input field e.g:

<input type="hidden" name="_method" value="delete">



回答2:


Method not allowed is an HTTP Status code 405, and it usually translates to the lack of HTTP Verb matching that endpoint

Edit: Also, check this

e.g.

All of these SHOULD mean different things and DO different things. If you make a Request to the last url but the url isn't registered (on your routes files or wherever you place them), then that's the error it returns because it matches the name but not the verb

POST   url.com/user

GET    url.com/user

PUT    url.com/user

PATCH  url.com/user

DELETE url.com/user



回答3:


try this package CORS Middleware for Laravel 5



来源:https://stackoverflow.com/questions/51396726/laravel-api-how-to-accept-delete-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!