简体   繁体   English

我想将 JSON 数据带到 Golang 使用 GIN 制作的服务器

[英]I want to bring JSON data to server made by Golang using GIN

I am making web application using Golang with GIN.我正在使用带有 GIN 的 Golang 制作 Web 应用程序。 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.当 Ajax 类型为GET ,我可以带 json 数据,但是当 Ajax 类型为POST ,我不知道如何将 json 数据发送到 GO 服务器。 I used method PostForm() and GetPostForm() , But It is not working.我使用了方法PostForm()GetPostForm() ,但它不起作用。 Plz help me.请帮助我。

Here is my code:这是我的代码:

join.js加入.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 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 .提交表单时,服务器期望的内容类型是application/x-www-form-urlencoded If the inut type="file" for uploading files is used the content type should be multipart/form-data如果使用 inut type="file" 上传文件,则内容类型应为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.将内容类型从application/json更改为application/x-www-form-urlencoded将使服务器/后端识别作为表单数据传递的数据,这将允许使用 c.PostForm 检索字段。

link to the w3.org spec for forms链接到 w3.org 表单规范

I believe the gin.Context.PostForm() functions are for accessing application/x-www-form-url-encoded data.我相信gin.Context.PostForm()函数用于访问application/x-www-form-url-encoded数据。 To accept application/json data one can use the gin.Context.BindJSON function for binding the values in the request.要接受application/json数据,可以使用gin.Context.BindJSON函数来绑定请求中的值。 This can handle data in either json or form-url-encoded (although in the example below the struct is only annotated for handling json).这可以处理 json 或 form-url-encoded 中的数据(尽管在下面的示例中,该结构仅被注释用于处理 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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM