get 传 json 数据
Posted dy blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了get 传 json 数据相关的知识,希望对你有一定的参考价值。
$.ajax({ type: ‘GET‘, url: "/list/guidance", contentType: "application/json", data: JSON.stringify({ pageSize: 10, pageIndex: 0, }), success: function (result,status,xhr) { $(".content").html(result); }, error: function(xhr,status,error) { alert(xhr,status,error); }, })
//从request中获取request data struct,Content-Type须为application/json func GetReqJSON(r *http.Request, reqJson interface{}) (code int, msg, debugMsg string) { if strings.Index(r.Header.Get("Content-Type"), "application/json") == -1 { return CodeContentTypeNotJSON, MsgContentTypeNotJSON, "Content-Type should be application/json,Content-Type: " + r.Header.Get("Content-Type") } var body []byte var err error if r.Method == "GET" { query, err := url.QueryUnescape(r.URL.RawQuery) //net/url包解码 if err != nil { return CodeReadRequestBodyError, MsgReadRequestBodyError, err.Error() } body = []byte(query) } else { body, err = ioutil.ReadAll(r.Body) if err != nil { return CodeReadRequestBodyError, MsgReadRequestBodyError, err.Error() } } err = json.Unmarshal(body, &reqJson) if err != nil { return CodeRequestJsonError, MsgRequestJsonError, err.Error() } //logs.DebugPrint(reqJson) return CodeSuccess, MsgSuccess, "" }
以上是关于get 传 json 数据的主要内容,如果未能解决你的问题,请参考以下文章