1 回答
TA贡献1805条经验 获得超9个赞
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 {
Username string `json:"username" form:"username" query:"username"`
Email string `json:"email" form:"email" query:"email"`
Password string `json:"password" form:"password" query:"password"`
}
func SignUp(c echo.Context) error {
form := new(SignUpForm)
if err := c.Bind(form); err != nil {
return c.JSON(http.StatusBadRequest, err)
}
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)
}
}
- 1 回答
- 0 关注
- 84 浏览
添加回答
举报