如何从浏览器发送 HTTP DELETE 请求?
有没有办法使用 xmlhttprequest
或类似的东西从网站发送 DELETE
请求?
Is there a way to send a DELETE
request from a website, using xmlhttprequest
or something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如上面提到的, jQuery 将通过以下语法为您完成此操作:
As someone mentioned above, jQuery will do this for you, via the following syntax:
您可以在此处测试您的浏览器是否已实现 DELETE
假设 req 是 XMLHttpRequest 对象,则代码将是
req.open("DELETE", uri, false)
;You can test if your browse has DELETE implemented here
Supposing req is a XMLHttpRequest object, the code would be
req.open("DELETE", uri, false)
;我在其中一个项目上使用 fetch API:
我之前定义了
deleteEndpoint
、id
和token
。I use fetch API on one of the projects:
I have
deleteEndpoint
,id
andtoken
previously defined.这可以通过
jQuery
来完成,如果您不介意对框架的依赖。我相信 jQuery 使用 XmlHttpRequest 来执行此操作。您可以使用$.ajax
函数,并将type
参数设置为DELETE
。请注意,并非所有浏览器都支持 HTTP DELETE 请求。
This can be done with
jQuery
, if you don't mind the dependence on a framework. I believejQuery
usesXmlHttpRequest
to perform this action. You'd use the$.ajax
function with thetype
parameter set toDELETE
.Please note that not all browsers support HTTP DELETE requests.
我相信由于浏览器支持不稳定,PUT 和 DELETE 都没有实现(在 Prototype 的情况下)。
I believe that both PUT and DELETE are not implemented (in the case of Prototype) due to flaky browser support.
您可以使用 php 来执行此操作:
设置 XMLHTTPRequst 来调用删除指定文件的 php 脚本,然后将要删除的文件名传递给 php 脚本并让它执行其操作。
You can use php to do this:
setup your XMLHTTPRequst to call a phpscript that deletes a named file, and then pass the filename that you intend to be removed to the php script and let it do its business.