亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 Angular 表單提交到 Go API 不斷發布 Nil

將 Angular 表單提交到 Go API 不斷發布 Nil

Go
梵蒂岡之花 2023-06-12 17:21:36
我正在嘗試從我的 Angular 前端發布到我的 Go API,無論我做什么,它都會將 nil 發布到 Go API。當我測試變量是否發送到 Angular 訂閱者時,它顯示它們是,但由于某種原因它不會發布到這個簡單的 Go 端點這是我的代碼任何幫助將不勝感激:去APIpackage mainimport (    "encoding/json"    "fmt"    "github.com/dgrijalva/jwt-go"    "log"    "net/http"    "time")func main() {    http.HandleFunc("/signin", indexHandler)    http.HandleFunc("/welcome", Welcome)    http.HandleFunc("/refresh", Refresh)    // start the server on port 8000    log.Fatal(http.ListenAndServe(":8001", nil))}func setupResponse(w *http.ResponseWriter, req *http.Request) {    (*w).Header().Set("Access-Control-Allow-Origin", "*")    (*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")    (*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")}func indexHandler(w http.ResponseWriter, req *http.Request) {    setupResponse(&w, req)    if (*req).Method == "POST" {        return    }    fmt.Println(req.Header)    fmt.Println(req.ParseForm())    fmt.Println(json.NewDecoder(req.Body))}這是我在 Go 中的輸出map[User-Agent:[Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Mobile Safari/537.36] Access-Control-Request-Headers:[ access-control-allow-headers,access-control-allow-origin,content-type] Referer:[ http://localhost:4200/login?returnUrl=%2Fdashboard]接受語言:[en-US,en;q =0.9] 連接:[keep-alive] 接受:[ / ] 接受編碼:[gzip, deflate, br] 訪問控制請求方法:[POST] 來源:[ http://localhost:4200]] & {{} [] {[] 0 { false [] false 0 0} { false [] false 0 0} { } false false} 0 0 { false [] false 0 0} 0 []}然后我在 Angular 中收到一個 CORS 錯誤,它是:從來源“ http://localhost: 4200 ”訪問位于“ http://localhost: 8001/signin ”的 XMLHttpRequest 已被 CORS 策略阻止:Access-Control 不允許請求標頭字段 access-control-allow-origin -在飛行前響應中允許標頭。如果有人能幫助我,我將不勝感激。我是 Go 和 Angular 的新手,并且仍在學習如何讓它們相互交互。
查看完整描述

1 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

像這樣進行更改:


func indexHandler(w http.ResponseWriter, req *http.Request) {

    setupResponse(&w, req)

    if (*req).Method == "POST" {

          fmt.Println(req.Header)


          fmt.Println(req.ParseForm())

          fmt.Println(json.NewDecoder(req.Body))

    }

}

角度客戶端


login(username: string, password: string) {


    const httpOptions = {

      headers: new HttpHeaders({ 'Content-Type': 'application/json charset=utf-8'})

    };

   return this.http.post(`${environment.APIEndpoint}/signin`, `{"username":username, "password":password}`, httpOptions).pipe(map(user => {

        // login successful if there's a jwt token in the response

        if (user && user) {

          // store user details and jwt token in local storage to keep user logged in between page refreshes

          localStorage.setItem('currentUser', JSON.stringify(user));

        }


        return user;

      }));

  }


查看完整回答
反對 回復 2023-06-12
  • 1 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號