Need some help on forms & single-page app: New to golang, html, and jquery

polaris · · 736 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;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&#39;m new to these languages, and I&#39;m having some difficulty getting the data to go across the wire as I want.</p> <p>I&#39;ve got a form that looks like this:<br/> note: I&#39;m using the ace template engine, hence the truncated HTML </p> <pre><code>div#my-page action=&#34;/myurl&#34; method=&#34;POST&#34; form#my-form div label Field Label input type=&#34;text&#34; name=&#34;fieldname&#34; required= div button type=&#34;submit&#34; 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&#39;s messed up my single-page app by navingating me to &#34;/myurl&#34;. So I don&#39;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>$(&#34;#my-form&#34;).submit(function(event){ event.preventDefault(); var fd = new FormData($(this)[0]); $.ajax({ method: &#34;POST&#34;, url: &#34;/myurl&#34;, 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&#39;ve got some sort of naive mistake here, but all of the online sources I&#39;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&#39;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

736 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传