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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料