英文:
POST request treated as OPTIONS on beego framework
问题
我正在使用beego框架作为我的API框架,并在客户端使用AngularJS。
我已经正确设置了所有的CORS设置。我可以进行GET请求。但是,当我尝试进行POST请求时,beego将其视为OPTIONS请求。它还会抛出一个警告:multiple response.WriteHeader calls
。可能出了什么问题?
我的beego CORS设置如下:
func init() {
orm.RegisterDataBase("default", "mysql", "root:@tcp(127.0.0.1:3306)/fakeapi")
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "DELETE", "PUT", "PATCH", "POST"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
}
我的AngularJS请求如下:
var transaction = $http.post(BASE_URL + "transaction", transactionData);
return $q.all([transaction]).then(function(response) {
console.log(response);
});
我的系统环境:
Ubuntu 14.04
beego: 1.4.2
bee: 1.2.4
angularJS: 1.3.12
英文:
I'm using beego framework as my API framework and AngularJS on the client.
I have set all CORS setting correctly. I can do GET request. But, when i try to POST, beego treat is as OPTIONS request. It also throw a warning: multiple response.WriteHeader calls
. what could possibly wrong?
my beego CORS setting:
func init() {
orm.RegisterDataBase("default", "mysql", "root:@tcp(127.0.0.1:3306)/fakeapi")
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "DELETE", "PUT", "PATCH", "POST"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
}
My ANgularJS request
var transaction = $http.post(BASE_URL + "transaction", transactionData);
return $q.all([transaction]).then(function(response) {
console.log(response);
});
my system:
Ubuntu 14.04
beego: 1.4.2
bee: 1.2.4
angularJS: 1.3.12
答案1
得分: 2
可能是因为一个问题/拉取请求目前正在等待合并到主分支中:问题 912
> 如果没有这一行,一切都很好:router.go#L861
这似乎与提交 3bb4d6f一致,其中显示:
// 如果手动设置了状态码,则写入状态码
// 设置为0以防止“多个response.WriteHeader调用”
(因此,router.go
确实设置了状态码,因此出现了错误消息)
提交 f962457应该解决这个问题,但尚未合并。
另一个问题 904提到无法检索先前在会话引擎中注册的会话数据。
也许Session.on标志可以帮助。
英文:
That might because of an issue/pull request currently pending to be merged into master: issue 912
> Without this line everything is fine:: router.go#L861
That seems to be in line with commit 3bb4d6f which shows:
// Write status code if it has been set manually
// Set it to 0 afterwards to prevent "multiple response.WriteHeader calls"
(and router.go
do set a status, hence the error message)
Commit f962457 is supposed to solve this issue, but isn't merged yet.
The other issue 904 mentions something about being unable to retrieve the Session data previously registered in the Session Engine.
Maybe Session.on flag can help.
答案2
得分: 0
我这样处理,希望能帮到你
import (
_ "info_apoyo/routers"
"github.com/astaxie/beego"
"github.com/astaxie/beego/plugins/cors"
)
func main() {
if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "DELETE", "PUT", "PATCH"},
AllowHeaders: []string{"Origin", "content-type", "Access-Control-Allow-Origin"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin"},
AllowCredentials: true,
}))
beego.Run()
}
英文:
I handle it like that, I hope it helps
import (
_ "info_apoyo/routers"
"github.com/astaxie/beego"
"github.com/astaxie/beego/plugins/cors"
)
func main() {
if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "DELETE", "PUT", "PATCH"},
AllowHeaders: []string{"Origin", "content-type", "Access-Control-
Allow-Origin"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-
Origin"},
AllowCredentials: true,
}))
beego.Run()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论