Javascript 如何从浏览器发送http删除请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1908693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:51:10  来源:igfitidea点击:

How can I send a http delete request from browser?

javascripthttphttp-delete

提问by Kalvaresin

Is there a way to send a DELETE request from a website, using xmlhttprequestor something similar?

有没有办法从网站,使用xmlhttprequest或类似的东西发送 DELETE 请求?

回答by Mike Trpcic

As someone mentioned above, jQuerywill do this for you, via the following syntax:

正如上面提到的,jQuery将通过以下语法为您完成此操作:

$.ajax({
    type: "DELETE",
    url: "delete_script.php",
    data: "name=someValue",
    success: function(msg){
        alert("Data Deleted: " + msg);
    }
});

回答by jbochi

You can test if your browse has DELETE implemented here

您可以测试您的浏览是否在此处实现DELETE

Supposing req is a XMLHttpRequest object, the code would be req.open("DELETE", uri, false);

假设 req 是一个 XMLHttpRequest 对象,代码将是req.open("DELETE", uri, false);

回答by Tim S. Van Haren

This can be done with jQuery, if you don't mind the dependence on a framework. I believe jQueryuses XmlHttpRequestto perform this action. You'd use the $.ajaxfunction with the typeparameter set to DELETE.

jQuery如果您不介意对框架的依赖,这可以通过 来完成。我相信jQuery用于XmlHttpRequest执行此操作。您将使用参数设置为的$.ajax函数。 typeDELETE

Please note that not all browsers support HTTP DELETE requests.

请注意,并非所有浏览器都支持 HTTP DELETE 请求。

回答by Damjan Pavlica

I use fetch API on one of the projects:

我在其中一个项目上使用 fetch API:

fetch(deleteEndpoint, {
  method: 'delete',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({id: id, token: token})
})

I have deleteEndpoint, idand tokenpreviously defined.

我有deleteEndpointid并且token之前定义过。

回答by robjmills

I believe that both PUT and DELETE are not implemented (in the case of Prototype) due to flaky browser support.

我相信 PUT 和 DELETE 都没有实现(在 Prototype 的情况下),因为浏览器支持不稳定。

回答by MikeEL

You can use php to do this:

您可以使用 php 来执行此操作:

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.

设置您的 XMLHTTPRequst 以调用删除命名文件的 phpscript,然后将您打算删除的文件名传递给 php 脚本并让它完成它的工作。