duanqiang2617 2017-01-10 01:52
浏览 125

Golang + Ajax发布数据

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("category")
              fmt.Println(frmCategory)
}

func main() {

          router:=httprouter.New()
          router.POST("/addProduct",addProduct)

}

AJAX code

<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
              });
        }
});

Console output

Toys

Toys

Toys

Toys

Toys

Toys

Toys
  • 写回答

1条回答 默认 最新

  • dongqiancui9194 2017-01-10 02:08
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了