gpt4 book ai didi

ajax - 使用 GO 和 Martini 提交表单

转载 作者:数据小太阳 更新时间:2023-10-29 03:29:04 25 4
gpt4 key购买 nike

我是 GO 和马提尼套餐的新手。我现在想做的是使用 AJAX 提交表单。问题是 go 返回整个 html 文件。我不知道是否有错误,因为没有错误返回。我需要测试我的表单是否成功提交数据,因为我将使用它向 API 发送 POST 数据。现在只需要知道我的表单是否成功传递了数据。

我有这个代码。

GO代码:

type UserSignup struct {
Email string `form: "email"`
}

func signup_user(email string) UserSignup {
return UserSignup {
Email : email
}
}

AJAX 调用:

$.ajax({
url: '/signup',
type: 'POST',
success: function(data) {
console.log(data);
},
error: function(result) {
//general div to handle error messages
console.log(result.responseText);
}
});

MTPL代码:

<form class="form-signup" action="/signup">
<input type="text" value="Email" name="email" class="signup-email" id="signup-email" onClick="this.setSelectionRange(0, this.value.length)">
<input type="submit" value="Go" id="signup-go">
</form>

谢谢。

最佳答案

表单值实际上来自 http.Request,除非您使用 Binding,否则它们不会传递到处理程序。

使用请求:

func signup_user(r *http.Request) {

email := r.FormValue("email")

return email

}

使用马提尼绑定(bind):

func signup_user(us UserSignup, r *http.Request) {

email := us.Email

return email

}

关于ajax - 使用 GO 和 Martini 提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26043355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com