使用`action`属性提交表单与使用XMLHttpRequest发送表单数据有何不同?

huangapple go评论70阅读模式
英文:

How does submitting form using action attribute differ from sending form data using XMLHttpRequest?

问题

我正在尝试使用XMLHttpRequest提交我的表单值。

HTML:

<form id="form" method="post" action="../myapi/submitRequest">
    <input type="text" name="type" placeholder="Type">
    <input type="text" name="eventDate" placeholder="Date">
    <input type="text" name="amount" placeholder="Amount">
    <input type="file" name="reciept">
    <input type="submit">
</form>

在Java的某处:

System.out.println(request.getParameter("type"));
System.out.println(request.getParameter("eventDate"));
System.out.println(request.getParameter("amount"));
System.out.println(request.getParameter("reciept"));

这会打印表单字段的值。工作得非常好!

但是,

HTML:

<form id="form">
    <input type="text" name="type" placeholder="Type">
    <input type="text" name="eventDate" placeholder="Date">
    <input type="text" name="amount" placeholder="Amount">
    <input type="file" name="reciept">
    <input type="submit">
</form>

JavaScript:

const form = document.getElementById("form");

form.addEventListener('submit', function(event){
    let xhr = new XMLHttpRequest();
    xhr.open("POST", "../myapi/submitRequest", true);

    let fd = new FormData(form);

    xhr.send(fd);
});

在Java的某处:

System.out.println(request.getParameter("type"));
System.out.println(request.getParameter("eventDate"));
System.out.println(request.getParameter("amount"));
System.out.println(request.getParameter("reciept"));

当我这样做时,所有参数值都为null

我做错了什么吗?

英文:

I am trying to submit my form values using XMLHttpRequest.

HTML:

&lt;form id=&quot;form&quot; method=&quot;post&quot; action=&quot;../myapi/submitRequest&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;type&quot; placeholder=&quot;Type&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;eventDate&quot; placeholder=&quot;Date&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;amount&quot; placeholder=&quot;Amount&quot;&gt;
    &lt;input type=&quot;file&quot; name=&quot;reciept&quot;&gt;
    &lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;

Somewhere in Java:

System.out.println(request.getParameter(&quot;type&quot;));
System.out.println(request.getParameter(&quot;eventDate&quot;));
System.out.println(request.getParameter(&quot;amount&quot;));
System.out.println(request.getParameter(&quot;reciept&quot;));

This prints the form fields value. Works perfectly!

But,

HTML:

&lt;form id=&quot;form&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;type&quot; placeholder=&quot;Type&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;eventDate&quot; placeholder=&quot;Date&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;amount&quot; placeholder=&quot;Amount&quot;&gt;
    &lt;input type=&quot;file&quot; name=&quot;reciept&quot;&gt;
    &lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;

JavaScript:

const form = document.getElementById(&quot;form&quot;);

form.addEventListener(&#39;submit&#39;,function(event){

    let xhr = new XMLHttpRequest();
    xhr.open(&quot;POST&quot;,&quot;../myapi/submitRequest&quot;,true);

    let fd = new FormData(form);

    xhr.send(fd);

});

Somewhere in Java:

System.out.println(request.getParameter(&quot;type&quot;));
System.out.println(request.getParameter(&quot;eventDate&quot;));
System.out.println(request.getParameter(&quot;amount&quot;));
System.out.println(request.getParameter(&quot;reciept&quot;));

When I do this, I get null for all parameter values.

Am I doing something wrong?

答案1

得分: 1

我想我找到了为什么它会这样行为的原因。当我使用action属性提交表单数据时,我没有设置enctype属性。因此,当我提交表单时,内容类型是application/x-www-form-urlencoded。而当我使用JavaScript发送时,内容类型被设置为multipart/form-data; boundary=----WebKitFormBoundaryfxmNAuz7ZhhuUcZn

我查找了一下,发现如果内容类型不是application/x-www-form-urlencodedrequest.getParameter()将返回null。所以,这就是为什么我得到那些空值的原因!

有用的帖子

https://stackoverflow.com/questions/3337056/convenient-way-to-parse-incoming-multipart-form-data-parameters-in-a-servlet

英文:

I think I found out why it was acting this way. When I was submitting the form data using the action attribute, I did not set the enctype attribute. So, when I was submitting the form, the content type was application/x-www-form-urlencoded. And when I was sending it using javascript, the content type was set to multipart/form-data; boundary=----WebKitFormBoundaryfxmNAuz7ZhhuUcZn.

I looked around and found out that request.getParameter() will return null if the content-type is not application/x-www-form-urlencoded. So, that was why I was getting those null values!

Helpful Posts

https://stackoverflow.com/questions/3337056/convenient-way-to-parse-incoming-multipart-form-data-parameters-in-a-servlet

huangapple
  • 本文由 发表于 2020年10月18日 14:19:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64410331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定