为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我无法获得体形 Angular

为什么我无法获得体形 Angular

Go
慕森王 2023-05-15 15:45:36
我使用 angular 来获取信息注册 html 并且我发布到路径 api 它工作但打印单词“工作”并且我无法在正文中获得价值并且我测试路径后使用邮递员它显示名字............ ..................................................... ..................................................... ..................................服务器.gopackage mainimport (    "github.com/gorilla/handlers"    "github.com/gorilla/mux"    "net/http")func main() {    router := mux.NewRouter()    router.HandleFunc("/register", Createuser).Methods("POST")    headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})    methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE"})    origins := handlers.AllowedOrigins([]string{"*"})    http.ListenAndServe(":12345", handlers.CORS(headers, methods, origins)(router))}func Createuser(writer http.ResponseWriter, request *http.Request) {    request.ParseForm()    print(request.FormValue("firstName"))    print("work")}
查看完整描述

1 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

我认为可能是您的 go 服务器期望content-type: application/x-www-form-urlencoded


https://golang.org/pkg/net/http/#Request.ParseForm


但是 Angular 将表单作为 json 对象提交。


要调试正在发生的事情,请检查从 chrome 到后端的传出响应通过在 chrome devtools 的设置菜单中启用“Log XmlHttpRequest”来执行此操作。


然后您应该能够确定正文是 JSON。


尝试使用它来解析正文


func test(rw http.ResponseWriter, req *http.Request) {

    decoder := json.NewDecoder(req.Body)

    var t test_struct

    err := decoder.Decode(&t)

    if err != nil {

        panic(err)

    }

    log.Println(t.Test)

}


查看完整回答
反对 回复 2023-05-15
  • 1 回答
  • 0 关注
  • 84 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信