我能否以超文本形式使用PUT方法,将数据从表格中传送到服务器?
I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....
我能否以超文本形式使用PUT方法,将数据从表格中传送到服务器?
XHTML 1.x forms only support GET and POST. GET and POST are the only allowed values for the "method" attribute.
根据,“超标准,请not。 方法属性的唯一有效数值是get/code>和
post/code>,与GET和POST HTTP方法对应:
<form methods=“put”>
为无效的超文本,将按<form>
处理,即发出GET申请。
相反,许多框架只是使用POST参数来绕过吉大港山区办法:
<form method="post" ...>
<input type="hidden" name="_method" value="put" />
...
当然,这要求服务器的冲破。
我能否使用html形式的“Put”方法,从超文本格式向服务器发送数据?
是的,但铭记这不会产生PUT,而是GET的要求。 如果您对<代码>method属性;form> tag使用无效价值,则浏览器将使用违约值<代码>get/code>。
超文本形式(最多为超文本4(草案5)和超文本1)只支持GET和POST作为吉卜赛人的申请方法。 这方面的一个工作是利用服务器阅读的隐蔽形式领域和相应发出的要求,通过POST寻找其他方法。 <
http://www.w3.org/MarkUp/"rel=“noretinger”>X 计划支持GET、POST、PUT和DELETE表格,但投进rel=“noreferer”>X5
否则,你可以提供一种表格,但不能提交表格,而是使用PUT方法,用 Java本制造和焚烧XMLHttpRequest。
<><>>>> 隐蔽的实地工作around
几个网络框架使用以下简单技术:
add a hidden _method
parameter to any form that is not GET or POST:
<input type="hidden" name="_method" value="PUT">
可以通过使用超文本制作助手方法在框架中自动做到这一点。
fix the actual form method to POST (<form method="post"
)
<程序代码>_method上载服务器,与发送该方法而不是实际操作的POST一样。
你可以做到这一点:
form_tag
@method("PATCH")
Rationale / history of why it is not possible in electronic RUS: https://softwareengineering.stack Exchange.com/questions/114156/why-there-are-no-put-and-delete-methods-in-html-forms
http://www.ohchr.org。
<form method="post" ...>
@csrf
@method( put )
...
</form>
不幸的是,现代浏览器没有向吉大港山区项目项目项目项目提供本地支持。 为了克服这一限制,确保您的超文本形式的方法属性是“后方”,然后在您的超文本形式上添加一个超重参数,如:
<input type="hidden" name="_METHOD" value="PUT"/>
检验你的请求,你可以使用“Postman”的 go角。
1. 制定方法
<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>
之后,联合材料公司采取行动,执行所期望的方法:
<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表上的带子:
<script>
document.querySelectorAll( "[name=remove_data]" ).forEach( element => {
var button = element;
button.addEventListener( "click", deleteMethod );
</script>
- - - - - - - - - - - -
The article https://blog.garstasio.com/you-dont-need-jquery/ajax/ 帮助我!
除此之外,你可以确定“Method”后的功能,并找到处理“POST”和“GET”的方法,因为你喜欢浏览器的违约行为。 你可以做你想要使用<代码> 所在地:reload(<>”,如显示成功修改或成功删除的信息。
=-=-==-=========================================================================================================================================================================================================================================================
If you are using nodejs, you can install the package method-override
that lets you do this using a middleware.
Link to documentation: http://expressjs.com/en/resources/middleware/method-override.html
在安装后,我必须做以下工作:
var methodOverride = require( method-override )
app.use(methodOverride( _method ))
我写了npm Pack,称为html-form-enhancer 。 通过将表格投放到你的超文本来源,它从<代码>GET和>的外法文本上取而代之,并添加<代码> 申请/json
序号。
<script type=module" src="html-form-enhancer.js"></script>
<form method="PUT">
...
</form>
I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....
I have a div <div id="masterdiv"> which has several child <div>s. Example: <div id="masterdiv"> <div id="childdiv1" /> <div id="childdiv2" /> <div id="...
I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...
<form><input type="file" name="first" onchange="jsFunction(2);"> <input type="file" name="second" onchange="jsFunction(3);"</form> Possible to pass just numbers to the js ...
So I ve got a menu with a hover/selected state and it loads fine in IE6/IE7. However when I scroll down the page and put the element outside of the viewport and then back in I get a broken image! I ...
I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!