gpt4 book ai didi

ajax - Beego不接受ajax参数

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

我正在尝试使用 VueJS 向使用 Beego 框架 (GoLang) 编写的应用程序发出简单的 POST 请求,但该应用程序没有看到任何输入请求。当我使用标准表单请求(无 ajax)时一切正常。这是我的 VueJS 代码:

storePost: function(event) {
axios.post("/api/posts/store", {body: "test"}).then(function(response) {
if (response.data.status == 200) {
this.posts.push(response.data.data);
}else {
console.log("error");
}
}, function(response){
console.log("error");
});
}

这是我的 Beego 代码:

// router.go
beego.Router("/api/posts/store", &controllers_API.PostsController{}, "post:Store")

// PostsController.go
func (this *PostsController) Store() {
fmt.Println(this.GetString("body"))

// some irrelevant code which handles the response...
}

fmt.Println 始终不打印任何内容。当我使用标准格式时,fmt.Println 会毫无问题地打印出 body 的值。

最佳答案

似乎 Beego 只接受带有此 header 的数据:'Content-Type': 'multipart/form-data' 所以在我添加该 header 后一切正常。因为我不知道如何使用 axios 来做到这一点,所以我切换到 vue-resource 这是与 Beego 一起使用的示例代码:

this.$http.post("/", {test: "test"}, {
emulateJSON: true
});

现在你可以这样打印了:

fmt.Println(this.GetString("test"))

希望对大家有帮助

关于ajax - Beego不接受ajax参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41680798/

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