第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

GoLang/Javascript:在 JSON POST 上清空 postForm

GoLang/Javascript:在 JSON POST 上清空 postForm

Go
蕪湖不蕪 2021-12-27 15:27:15
我正在嘗試將 JSON 數(shù)據(jù)從 javascript 頁(yè)面 POST 到 golang 服務(wù)器,但我無(wú)法在兩端使用 SO 接受的答案找到任何 JSON 數(shù)據(jù)的痕跡。這篇文章展示了我在 Javascript 中發(fā)布我的 JSON的方式,這篇文章展示了我嘗試在 Go 中處理這個(gè) JSON 的方式。//js json post sendvar request = new XMLHttpRequest();request.open('POST', 'http://localhost:8080/aardvark/posts', true);request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');var data = {hat: "fez"};request.send(JSON.stringify(data));//Go json post responsefunc reply(w http.ResponseWriter, r *http.Request) {    w.Header().Set("Access-Control-Allow-Origin", "*")    w.Header().Set("Access-Control-Allow-Credentials", "true")    w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")    w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")    if err := r.ParseForm(); err != nil {        fmt.Println(err);    }    //this is my first impulse.  It makes the most sense to me.    fmt.Println(r.PostForm);          //out -> `map[]`  would be `map[string]string` I think    fmt.Println(r.PostForm["hat"]);   //out -> `[]`  would be `fez` or `["fez"]`    fmt.Println(r.Body);              //out -> `&{0xc82000e780 <nil> <nil> false true {0 0} false false false}`    type Hat struct {        hat string    }    //this is the way the linked SO post above said should work.  I don't see how the r.Body could be decoded.    decoder := json.NewDecoder(r.Body)    var t Hat       err := decoder.Decode(&t)    if err != nil {        fmt.Println(err);    }    fmt.Println(t);                  //out -> `{ }`}我不確定從這里還可以嘗試什么。我應(yīng)該改變什么才能使這項(xiàng)工作?
查看完整描述

1 回答

?
九州編程

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊

導(dǎo)出hat結(jié)構(gòu)Hat和 json 解碼的字段將起作用。


type Hat struct {

    Hat string // Exported field names begins with capital letters

}


查看完整回答
反對(duì) 回復(fù) 2021-12-27
  • 1 回答
  • 0 關(guān)注
  • 180 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)