dounangqie4819 2017-01-19 07:35
浏览 274
已采纳

我想使用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)
    })
  • 写回答

2条回答 默认 最新

  • doushoubu5360 2017-01-19 07:54
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀