如何使用Gin

时间:2019-06-08 01:54:31

标签: arrays post gin

我正在尝试使用Go / Gin Gonic从HTML表单中捕获Post值的数组-在PHP中,我将使用类似的东西:

var oneData = [];
    function addOne() {
        var len = oneData.length;
        oneData.push(getNowFormatDate(len));
    }
    function getNowFormatDate(len) {
        var date = new Date();
        date.setDate(date.getDate() + len);
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
    function submit() {
        $.post('/admin/v1/post/delete/batch', {ids: oneData}, function (json) {
            // some code
        }, 'json');
    }

发布数据:

(3) ["2019-06-08", "2019-06-09", "2019-06-10"]
0: "2019-06-08"
1: "2019-06-09"
2: "2019-06-10"
length: 3

我使用了两种方法,但无法接收id的值。

type selectData struct {
        Selection []int `form:"ids[]"`
    }
    var s selectData
    _ = ctx.Bind(&s)
    fmt.Println(s.Selection)
    selection := ctx.PostFormArray("ids")
    fmt.Println(selection)

结果

[GIN] 2019/06/08 - 09:50:51 | 200 | 0s | ::1 | POST /admin/v1/post/delete/batch
[]
[]

0 个答案:

没有答案