golang获取并解析ajax请求参数(带数组)

前台代码:(category是数组)

var data = {
			title: title,
			category: category.split(","),
			cancomment: cancomment,
			content: content,
			totalWords: totalWords
		};

		$.ajax({
			type: "POST",
			url: "/addArticle",
			traditional :true,
			// contentType: "application/json",
			// data: JSON.stringify(data),
			data: data,
			success: function(msg) {
				var msgObject = JSON.parse(msg);
				if(msgObject.Status === true) {
					// window.location.href = msgObject.Url;
				}
			}
		});
后台代码:

package controller

import (
	"encoding/json"
	"fmt"
	"github.com/gorilla/schema"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"myapp/module"
	"net/http"
	"strconv"
	"strings"
	"time"
)

type AddArticleResult struct {
	Content  module.Article
	Status   bool
	IsAuthor string
	Url      string
}

type Asd struct {
	Title      string
	Category   []string
	Cancomment string
	Content    string
	TotalWords int64
}

func AddArticle(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	if err != nil {
		fmt.Println("解析表单数据失败!")
	}

	var decoder = schema.NewDecoder()
	var asd Asd
	fmt.Println(r.PostForm)
	err = decoder.Decode(&asd, r.PostForm)
	if err != nil {
		fmt.Println("解码表单数据失败!")
		fmt.Println(err)
	}
	fmt.Println(asd)
	category := strings.Split(r.FormValue("category"), ",")
	fmt.Println("category---", asd.Category)

	for k, v := range asd.Category {
		fmt.Println(k, v)
	}

	// json.Unmarshal(data, &asd)

	// --------------------------------------------------------
	
	其他代码在这里省略。。。。。。。。。
	

	

	
}



©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值