我想使用GIN将JSON数据导入Golang制造的服务器

I am making web application using Golang with GIN. I can bring json data when Ajax type is GET, but When Ajax type is POST, I don't no how to send json data to GO server. I used method PostForm() and GetPostForm(), But It is not working. Plz help me.

Here is my code:

join.js

        var json_memberInfo = `{ 
            "id": "`+id+`",
            "password": "`+password+`",
            "name": "`+name+`",
            "birthday": "`+birthday+`",
            "tel": "`+tel+`",
            "email": "`+email+`"
        }`;

        var parse_memberInfo = JSON.parse(json_memberInfo);

        alert(json_memberInfo);

        $.ajax({
            url: "/join",
            type: "POST",
            data: parse_memberInfo,
            contentType: "application/json",
            success: function(result) {
                if (result) {
                    //alert("회원가입이 완료되었습니다!");
                }

                else {
                    //alert("에러가 발생하였습니다. 잠시 후에 다시 시도하여 주세요.");
                }
            }
      })

main.go

    router.POST("/join", func(c *gin.Context) {
        id := c.PostForm("id")
        password := c.PostForm("password")
        name := c.PostForm("name")
        birthday := c.PostForm("birthday")
        tel := c.PostForm("tel")
        email := c.PostForm("email")

        fmt.Println(id + " " + password + " " + name + " " + birthday + " " + tel + " " + email)
    })

When submitting a form the content type that the server will expect is application/x-www-form-urlencoded. If the inut type="file" for uploading files is used the content type should be multipart/form-data

Changing the content type from application/json to application/x-www-form-urlencoded will let the server/backend identify the data being passed as form data wich will allow for the retrieval of the fields using c.PostForm.

link to the w3.org spec for forms

I believe the gin.Context.PostForm() functions are for accessing application/x-www-form-url-encoded data. To accept application/json data one can use the gin.Context.BindJSON function for binding the values in the request. This can handle data in either json or form-url-encoded (although in the example below the struct is only annotated for handling json). A snippet of an example for this is:

type Member struct {
  Id          string     `json:"id"`
  Password    string     `json:"password"`
  Name        string     `json:"name"`
  Birthday    string     `json:"birthday"`
  Tel         string     `json:"tel"`
  Email       string     `json:"email"`
}

// various code

router.POST("/join", func(c *gin.Context) {
  var jsonData Member
  if c.BindJSON(&jsonData) == nil {
    fmt.Println(jsonData.Id + " " + jsonData.Password + " " + jsonData.Name + " " +
      jsonData.Birthday + " " + jsonData.Tel + " " + jsonData.Email)
  } else {
    // handle error
  }
}