猿问

Go Echo 没有从 Vue 获取 POST 正文

我有 SignUp 函数并试图获取由 Vue 框架发送的请求的主体,但它是空的。



type SignUpForm struct {

    Username string

    Email    string

    Password string

}


func SignUp(c echo.Context) error {

    form := SignUpForm{

        Username: c.FormValue("username"),

        Email:    c.FormValue("email"),

        Password: c.FormValue("password")}


    user := models.User{

        Username: form.Username,

        Email:    form.Email,

        Password: models.HashPassword(form.Password),

    }


    log.Printf("#####################")

    values, _ := c.FormParams()

    log.Printf("%v\n", values)

    log.Printf("%v", c.Response().Header())

    log.Printf("#####################")


    err := database.Connection().Create(&user).Error

    if err != nil {

        return c.JSON(http.StatusInternalServerError, err)

    } else {

        return generateJwtToken(c, user)

    }

}

看过


 sendForm: function() {

  var link = '/auth/sign_up'

  axios.post(link, {

    username: "test",

    email: "user@gmail.com",

    password: "password"

  })

  .then(response => {

    console.log(e.response)

  })

  .catch(e => {

    console.log(e.response)

  })

如果我使用 Postman,我会得到这个日志信息


2018/10/27 14:11:48 #####################

2018/10/27 14:11:48 map[email:[user@gmail.com] password:[password] username:[test]]

2018/10/27 14:11:48 map[Vary:[Origin] Access-Control-Allow-Origin:[*]]

2018/10/27 14:11:48 #####################

如果我尝试通过 Vue 发送,我什么也得不到


2018/10/27 14:14:55 #####################

2018/10/27 14:14:55 map[]

2018/10/27 14:14:55 map[Vary:[Origin] Access-Control-Allow-Origin:[*]]

2018/10/27 14:14:55 #####################

我完全确定这是 go/echo 问题,因为我能够在我的 Rails 应用程序中获取这些参数,因此 Vue 可以正确发送它们。


<ActionController::Parameters {"username"=>"test", "email"=>"user@gmail.com", "password"=>"password"

有任何想法吗?


杨魅力
浏览 85回答 1
1回答

Cats萌萌

axios确实发送为json但echo寻找x-www-form-urlencoded. 在您vue添加axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'或传递标题中的每个axios.post在 echo 中,您可以将c.Bind请求放入您的变量中,如下所示。这样你就可以收到json或form(邮递员进行测试和vue)type SignUpForm struct {&nbsp; &nbsp; Username string `json:"username" form:"username" query:"username"`&nbsp; &nbsp; Email&nbsp; &nbsp; string `json:"email" form:"email" query:"email"`&nbsp; &nbsp; Password string `json:"password" form:"password" query:"password"`}func SignUp(c echo.Context) error {&nbsp; &nbsp; form := new(SignUpForm)&nbsp; &nbsp; if err := c.Bind(form); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return c.JSON(http.StatusBadRequest, err)&nbsp; &nbsp; }&nbsp; &nbsp; user := models.User{&nbsp; &nbsp; &nbsp; &nbsp; Username: form.Username,&nbsp; &nbsp; &nbsp; &nbsp; Email:&nbsp; &nbsp; form.Email,&nbsp; &nbsp; &nbsp; &nbsp; Password: models.HashPassword(form.Password),&nbsp; &nbsp; }&nbsp; &nbsp; log.Printf("#####################")&nbsp; &nbsp; values, _ := c.FormParams()&nbsp; &nbsp; log.Printf("%v\n", values)&nbsp; &nbsp; log.Printf("%v", c.Response().Header())&nbsp; &nbsp; log.Printf("#####################")&nbsp; &nbsp; err := database.Connection().Create(&user).Error&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return c.JSON(http.StatusInternalServerError, err)&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return generateJwtToken(c, user)&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Go
我要回答