如何以HTML发送PUT/DELETE请求? [英] How do I send a PUT/DELETE request in HTML?

查看:152
本文介绍了如何以HTML发送PUT/DELETE请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PHP创建REST API,并且我了解可以通过 $ _ SERVER ['REQUEST_METHOD'] 捕获请求方法.

I'm making a REST API in PHP and I understand that I can capture the request method via $_SERVER['REQUEST_METHOD'].

但是,如何在浏览器中触发PUT/DELETE请求?

However, how do I trigger PUT/DELETE requests in the browser?

我无法想象更改form标记的method属性以指定除GET或POST之外的其他方法在所有浏览器上都可以使用...加上HTML标准无法识别它.

I can't imagine changing the method attribute of the form tag to specify a method other than GET or POST would work on all browsers...plus the HTML standard doesn't recognize it.

谢谢.

推荐答案

通常,这是由一个隐藏的表单域完成的,并在应用程序中处理(基本上不是Web服务器).

Usually this is done by a hidden form field, and handled in the application (not the webserver, by and large).

<input type="hidden" name="_method" value="put" />

因此,在这种情况下,您将使用一组简单的if-else语句来确定 _method 变量是否被覆盖(当然有效).我会用类似的东西:

So in this case you'd use a simple set of if-else statements to determine if the _method variable is overridden (validly, of course). I'd use something like:

$method = 'get';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
  if(isset($_POST['_method']
     && ($_POST['_method'] == 'PUT' || $_POST['_method'] == 'DELETE')) {
    $method = strtolower($_POST['_method']);
  } else {
    $method = 'post';
  }
}

这是确定您的应用程序或框架的请求类型的简单方法.

This would be a simple way to determine the request type for your application or framework.

这篇关于如何以HTML发送PUT/DELETE请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆