Home > Backend Development > Golang > 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?

王林
Release: 2024-02-14 12:27:09
forward
678 people have browsed it

为什么每当我尝试使用 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)
Copy after login
<a class="delete-button" href="/quote/delete?quote_id={{ .quoteid}}">delete</a>
Copy after login

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 :

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

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 <dialog>, 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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template