1. 程式人生 > >get 傳 json 數據

get 傳 json 數據

pri sta highlight ajax color des tty 解碼 http

        $.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 數據