ajax - Beego不接受ajax参数

标签 ajax go vue.js beego

我正在尝试使用 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/

相关文章:

javascript - 关于动态添加 PubNub channel 的查询

go - 列出服务

json - golang写的Highcharts数据[x,y]

javascript - 正确使用 Vue Watch

jquery - 尝试通过 AJAX 提交时出现 %5Bobject%20Object%5D(未找到 404)

jquery - Symfony2 : Add Collection via AJAX Outside of Symfony

javascript - Codeigniter 聊天应用程序未提交表单数据,并且还在 javascript 控制台中显示 500 内部服务器错误

html - 使用Golang的colly/goquery在html中查找文本

javascript - Object.entries 不是函数

javascript - 根据数组值显示 UL 元素