<p>I'm teaching myself all of this and making a single-page app. I want to have a few different views submit form data, and to capture each independently, so I want to have a javascript onclick send a POST to a different golang handler for each different form/screen.<br/>
I'm new to these languages, and I'm having some difficulty getting the data to go across the wire as I want.</p>
<p>I've got a form that looks like this:<br/>
note: I'm using the ace template engine, hence the truncated HTML </p>
<pre><code>div#my-page action="/myurl" method="POST"
form#my-form
div
label Field Label
input type="text" name="fieldname" required=
div
button type="submit" Submit Label
</code></pre>
<p>I get a POST that goes to my handler, and I can use r.FormValue to get the values as I expect. However, that's messed up my single-page app by navingating me to "/myurl". So I don't want to use the default submit.</p>
<p>If I add the folliwng jquery to intercept submit and handle it myself: </p>
<pre><code>$("#my-form").submit(function(event){
event.preventDefault();
var fd = new FormData($(this)[0]);
$.ajax({
method: "POST",
url: "/myurl",
data: fd,
contentType: false,
processData: false
});
})
</code></pre>
<p>I get to my handler, but when I call r.FormValue on any of the items in my form, I just get empty strings.</p>
<p>Clearly I've got some sort of naive mistake here, but all of the online sources I've found make it look like this approach should work to populate the response so that request.FormValue will work.</p>
<p>Any clues on what I'm doing wrong would be greatly appreciated!</p>
<hr/>**评论:**<br/><br/>gohacker: <pre><p>Open the developer console in your browser and look at what is actually posted.</p></pre>whorfin: <pre><p>Thanks! That helped me track down the issue I was facing.</p>
<p>Now onto the next set of things to learn.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传