dongyoulou4829 2017-01-16 16:26
浏览 155

Beego不接受Ajax参数

I'm trying to make simple POST request using VueJS to an application which is written in Beego framework (GoLang) but the application doesn't see any input request. Everything is ok when I use standard form request (no ajax). This is my VueJS code:

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");
    });
}

and this is my Beego code:

// 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 always prints nothing. When I use standard forms fmt.Println prints the value of the body with no problems.

  • 写回答

2条回答 默认 最新

  • dongma6326 2017-01-16 19:51
    关注

    It seems that Beego only accepts data with this header: 'Content-Type': 'multipart/form-data' so after I added that header everything was ok. Since I didn't know how to do that with axios I switched to vue-resource and this is the example code which works with Beego:

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

    Now you can print it like this:

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

    I hope this helps someone

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置