Home  >  Article  >  Backend Development  >  Why do I always get "Method not allowed" on my html template whenever I try to use http.MethodDelete?

Why do I always get "Method not allowed" on my html template whenever I try to use http.MethodDelete?

王林
王林forward
2024-02-14 12:27:09644browse

为什么每当我尝试使用 http.MethodDelete 时,我的 html 模板上总是出现“不允许使用方法”?

php editor Baicao encountered the problem of "method not allowed" when using http.MethodDelete, which may be caused by server settings or code logic issues. First, make sure your server is properly configured and has the DELETE method enabled. Secondly, check your code logic to make sure that your code does not restrict or intercept the method when handling DELETE requests. Also, check whether your form or link uses the DELETE method correctly. If the above checks are normal, the problem may be due to other reasons. It is recommended to check the server log to find more detailed error information.

Question content

I am using http.MethodDelete to try to remove quotes from my html template. Every time I press the delete button I get an error message.

I tried using this in my html template "Delete" but it still gives me the error.

Solution

I think you mean:

router.handlerfunc(http.methoddelete, "/quote/delete", app.quotedelete)
<a class="delete-button" href="/quote/delete?quote_id={{ .quoteid}}">delete</a>

By default, browsers follow links by sending get requests. It's not sending the delete request as you expected.

You can use javascript code to send a delete request.

It doesn't appear that you have any javascript code written in your project, so a quick fix is ​​to modify the server code to handle post requests and use a form to send the post request to the delete resource endpoint : p>

router.handlerfunc(http.methodpost, "/quote/delete", app.quotedelete)
<form action="/quote/delete?quote_id={{ .QuoteID }}" method="post">
  <button type="submit">Delete</button>
</form>

Please note that forms cannot be used to send delete requests. The documentation lists the methods allowed by the from element. Form elements from mdn:

method

http method for submitting the form. The only allowed methods/values ​​are (case insensitive):

  • post: post method; form data is sent as the request body.
  • get (default): get; form data appended to action url with ? separator. Use this method when the form has no side effects.
  • dialog: When the form is inside a38fd2622755924ad24c0fc5f0b4d412, closes the dialog box and causes the submit event to be fired on submission without submitting the data or clearing the form.

For the definition of http request method, please see http request method.

The above is the detailed content of Why do I always get "Method not allowed" on my html template whenever I try to use http.MethodDelete?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete