本文介绍了Laravel API,如何接受 DELETE 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用我的 API 来删除一些实体,我创建了我的控制器、我的方法和路由.它们工作正常,所有的 get 和 put/patch 方法都可以工作,但是我的删除方法和使用这个 api 的 Angular 应用程序抛出的错误,这里是错误:

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)

在我的 api 路由文件中,我设置了这个:

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 ?

这是我的路线(是的,方法在好的控制器中):

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

推荐答案

当您发布到删除 url 时,请确保您的发布请求中包含此数据:

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">

这篇关于Laravel API,如何接受 DELETE 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:57