Golang + ajax post data(Golang + ajax 发送 POST 数据)

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

Golang + ajax post data

问题

我有一个页面上有多个字段,没有使用表单设置,因为我想要能够单独对它们进行操作并使用ajax进行提交。我正在使用Go来处理提交的数据。然而,似乎会多次提交,因为在我的Go代码中,我将一个post变量输出到控制台进行测试,它会输出5次,这代表了提交数据中的变量数量。当我只是使用jquery的alert来显示提交的数据时,也出现了这种行为。我是否对ajax提交有什么误解,或者还有其他bug出现了?

Go代码

func addProduct(w http.ResponseWriter, r *http.Request, p httprouter.Params) {

          frmCategory:=r.PostFormValue("category")
          fmt.Println(frmCategory)
}

func main() {
       
          router:=httprouter.New()
          router.POST("/addProduct",addProduct)
          
}

AJAX代码

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js" type="text/javascript"></script>

    <!--<script src="http://code.jquery.com/jquery-1.4.1.min.js"></script> -->

    <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel='stylesheet' type='text/css'>

var postData="bcode="+ $('#bCodeID').val()+"&category="+$("#selected_category").val()+"&price="+priceModifier+"&description="+$('#itemDescription').val()+"&colorcode="+$('#SelectedColorCode').val();

$.ajax({
           
        url: '/addProduct',
        type: 'post',
        dataType: 'text',
        data : postData,
        success : function(data) {
              $('#warningBox').modal({
                  show: false
              });
        }
});

控制台输出

Toys

Toys

Toys

Toys

Toys

Toys

Toys
英文:

I've got a page with a number of fields, not in a form setup because I wanna be able to act on them individually and post with ajax. I am using Go to process the post data. However, it seems to post multiple times because in my Go code I am outputting a post variable to the console to test, and it dumps the output 5 times, which represents the number of variables in the post data. I also had this type of behavior when I just used a jquery alert to show the post data. Am I misunderstanding something about how ajax posts, or is there some other bug going on?

Go code

func addProduct(w http.ResponseWriter, r *http.Request, p httprouter.Params) {

	          frmCategory:=r.PostFormValue(&quot;category&quot;)
	          fmt.Println(frmCategory)
}

func main() {
       
          router:=httprouter.New()
          router.POST(&quot;/addProduct&quot;,addProduct)
          
}

AJAX code

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot; type=&quot;text/css&quot;&gt;
	&lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js&quot;&gt;&lt;/script&gt;

	&lt;script src=&quot;//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

	&lt;!-- &lt;script src=&quot;http://code.jquery.com/jquery-1.4.1.min.js&quot;&gt;&lt;/script&gt; --&gt;

    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;//netdna.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css&quot;&gt;
    &lt;link href=&quot;//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css&quot; rel=&#39;stylesheet&#39; type=&#39;text/css&#39;&gt;

var postData=&quot;bcode\=&quot;+ $(&#39;#bCodeID&#39;).val()+&quot;&amp;category\=&quot;+$(&quot;#selected_category&quot;).val()+&quot;&amp;price\=&quot;+priceModifier+&quot;&amp;description\=&quot;+$(&#39;#itemDescription&#39;).val()+&quot;&amp;colorcode\=&quot;+$(&#39;#SelectedColorCode&#39;).val();

$.ajax({
           
        url: &#39;/addProduct&#39;,
        type: &#39;post&#39;,
        dataType: &#39;text&#39;,
        data : postData,
        success : function(data) {
              $(&#39;#warningBox&#39;).modal({
                  show: false
              });
        }
});

Console output

Toys

Toys

Toys

Toys

Toys

Toys

Toys

答案1

得分: 1

我发现了这个错误。之前我在尝试不允许在模态对话框之外点击时,使用了$(document).click(function () { ... })包裹了我的所有JavaScript代码。一旦我移除了这部分代码,其他的问题都解决了。

英文:

I found the bug. Earlier I was experimenting with not allowing clicks outside of a modal dialog, and was using $(document).click(function () { ... } wrapped around all my jscript code. Once I removed that, everything else fixed itself.

huangapple
  • 本文由 发表于 2017年1月10日 09:52:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/41559937.html
匿名

发表评论

匿名网友

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

确定