我可以在HTML表单中使用PUT方法将数据从表单发送到服务器吗?


当前回答

根据HTML标准,你不能。方法属性的唯一有效值是get和post,对应于get和post HTTP方法。<form method="put">是无效的HTML,将被视为<form>,即发送一个GET请求。

相反,许多框架只是简单地使用POST参数来隧道HTTP方法:

<form method="post" ...>
  <input type="hidden" name="_method" value="put" />
...

当然,这需要服务器端展开。

其他回答

我写了一个名为“html-form-enhancer”的npm包。通过将它放入HTML源代码中,它可以使用GET和POST以外的方法来提交表单,还可以添加application/json序列化。

<script type=module" src="html-form-enhancer.js"></script>

<form method="PUT">
...
</form>

XHTML 1。x表单只支持GET和POST。GET和POST是唯一允许的值 “method”属性。

不幸的是,现代浏览器不提供对HTTP PUT请求的本机支持。为了解决这个限制,确保你的HTML表单的方法属性是“post”,然后添加一个方法覆盖参数到你的HTML表单,像这样:

<input type="hidden" name="_METHOD" value="PUT"/>

测试你的要求,你可以使用“邮差”谷歌chrome扩展

要设置PUT和DELETE方法,我执行如下操作:

<form
  method="PUT"
  action="domain/route/param?query=value"
>
  <input type="hidden" name="delete_id" value="1" />
  <input type="hidden" name="put_id" value="1" />
  <input type="text" name="put_name" value="content_or_not" />
  <div>
    <button name="update_data">Save changes</button>
    <button name="remove_data">Remove</button>
  </div>
</form>
<hr>
<form
  method="DELETE"
  action="domain/route/param?query=value"
>
  <input type="hidden" name="delete_id" value="1" />
  <input type="text" name="delete_name" value="content_or_not" />
  <button name="delete_data">Remove item</button>
</form>

然后JS执行所需的方法:

<script>
   var putMethod = ( event ) => {
     // Prevent redirection of Form Click
     event.preventDefault();
     var target = event.target;
     while ( target.tagName != "FORM" ) {
       target = target.parentElement;
     } // While the target is not te FORM tag, it looks for the parent element
     // The action attribute provides the request URL
     var url = target.getAttribute( "action" );

     // Collect Form Data by prefix "put_" on name attribute
     var bodyForm = target.querySelectorAll( "[name^=put_]");
     var body = {};
     bodyForm.forEach( element => {
       // I used split to separate prefix from worth name attribute
       var nameArray = element.getAttribute( "name" ).split( "_" );
       var name = nameArray[ nameArray.length - 1 ];
       if ( element.tagName != "TEXTAREA" ) {
         var value = element.getAttribute( "value" );
       } else {
       // if element is textarea, value attribute may return null or undefined
         var value = element.innerHTML;
       }
       // all elements with name="put_*" has value registered in body object
       body[ name ] = value;
     } );
     var xhr = new XMLHttpRequest();
     xhr.open( "PUT", url );
     xhr.setRequestHeader( "Content-Type", "application/json" );
     xhr.onload = () => {
       if ( xhr.status === 200 ) {
       // reload() uses cache, reload( true ) force no-cache. I reload the page to make "redirects normal effect" of HTML form when submit. You can manipulate DOM instead.
         location.reload( true );
       } else {
         console.log( xhr.status, xhr.responseText );
       }
     }
     xhr.send( body );
   }

   var deleteMethod = ( event ) => {
     event.preventDefault();
     var confirm = window.confirm( "Certeza em deletar este conteúdo?" );
     if ( confirm ) {
       var target = event.target;
       while ( target.tagName != "FORM" ) {
         target = target.parentElement;
       }
       var url = target.getAttribute( "action" );
       var xhr = new XMLHttpRequest();
       xhr.open( "DELETE", url );
       xhr.setRequestHeader( "Content-Type", "application/json" );
       xhr.onload = () => {
         if ( xhr.status === 200 ) {
           location.reload( true );
           console.log( xhr.responseText );
         } else {
           console.log( xhr.status, xhr.responseText );
         }
       }
       xhr.send();
     }
   }
</script>

定义了这些函数后,我向按钮添加了一个事件监听器,使表单方法请求:

<script>
  document.querySelectorAll( "[name=update_data], [name=delete_data]" ).forEach( element => {
    var button = element;
    var form = element;
    while ( form.tagName != "FORM" ) {
      form = form.parentElement;
    }
    var method = form.getAttribute( "method" );
    if ( method == "PUT" ) {
      button.addEventListener( "click", putMethod );
    }
    if ( method == "DELETE" ) {
      button.addEventListener( "click", deleteMethod );
    }
  } );
</script>

对于PUT表单上的remove按钮:

<script>
  document.querySelectorAll( "[name=remove_data]" ).forEach( element => {
    var button = element;
    button.addEventListener( "click", deleteMethod );
</script>

_ - - - - - - - - - - -

这篇文章https://blog.garstasio.com/you-dont-need-jquery/ajax/帮助我很多!

除此之外,您还可以设置postMethod函数和getMethod来处理POST和GET提交方法,而不是浏览器默认行为。你可以使用location.reload()做任何你想做的事情,比如显示成功更改或成功删除的消息。

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

JSFiddle: https://jsfiddle.net/enriquerene/d6jvw52t/53/

根据HTML标准,你不能。方法属性的唯一有效值是get和post,对应于get和post HTTP方法。<form method="put">是无效的HTML,将被视为<form>,即发送一个GET请求。

相反,许多框架只是简单地使用POST参数来隧道HTTP方法:

<form method="post" ...>
  <input type="hidden" name="_method" value="put" />
...

当然,这需要服务器端展开。