$.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, ""
}
原文:http://www.cnblogs.com/cdyboke/p/7375311.html