第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

golang 服務(wù)器上的 CORS 和 javascript 獲取前端

golang 服務(wù)器上的 CORS 和 javascript 獲取前端

Go
瀟湘沐 2022-04-20 16:59:44
我有一個(gè) golang HTTP 服務(wù)器,其代碼如下:    http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {    log.Println("New incoming request")    // Authenticate    if u, p, ok := r.BasicAuth(); ok {      log.Println("Success")      return    }    log.Println("Failed")我從一個(gè) JS 前端調(diào)用這個(gè) HTTP 端點(diǎn),這是一個(gè)部署在端口 3000 上的反應(yīng)應(yīng)用程序,使用代碼:      fetch('http://localhost:8080/login', {            method: 'post',            headers: {                'Authorization': 'Basic ' + btoa(authHeader),                'Content-Type': 'application/x-www-form-urlencoded',                'Access-Control-Allow-Origin': '*'            },                body: 'A=1&B=2'            })            .then(function (response) {                console.log("Authentication Success")            })            .catch(function (err) {                console.log("Authentication fail", err)            });上面的代碼失敗并顯示以下日志。在服務(wù)器端:New incoming requestFailed在瀏覽器上,在開(kāi)發(fā)者工具日志中:Fetch API cannot load http://localhost:8080/login. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 401. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.有人可以幫助解決身份驗(yàn)證問(wèn)題嗎?我不確定我是在服務(wù)器端遺漏了與 CORS 相關(guān)的內(nèi)容,還是在客戶端進(jìn)行了錯(cuò)誤的身份驗(yàn)證。有什么幫助嗎?謝謝。
查看完整描述

2 回答

?
滄海一幻覺(jué)

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊

Access-Control-Allow-Origin: *必須從服務(wù)器發(fā)送,而不是由客戶端發(fā)送。假設(shè)您在標(biāo)準(zhǔn)net/http處理程序函數(shù)中,請(qǐng)嘗試以下代碼:


func handler(w http.ResponseWriter, r *http.Request) {

    w.Header().Set("Access-Control-Allow-Origin", "*")

    if (r.Method == "OPTIONS") {

        w.Header().Set("Access-Control-Allow-Headers", "Authorization") // You can add more headers here if needed

    } else {

        // Your code goes here

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-04-20
?
慕姐8265434

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊

首先 - 您需要在處理程序中使用模式:


w.Header().Set("Access-Control-Allow-Origin", "*")

    if (r.Method == "OPTIONS") {

        w.Header().Set("Access-Control-Allow-Headers", "Authorization") // You can add more headers here if needed

    } else {

        // Your code goes here

    }

但在此之前,您需要在主“選項(xiàng)”中指定:


router.HandleFunc("/your_route/", your_method).Methods("POST", "OPTIONS")

這是因?yàn)槟臑g覽器執(zhí)行 2 個(gè)請(qǐng)求 - 首先檢查使用某些標(biāo)頭的能力(例如授權(quán)),下一步是發(fā)布數(shù)據(jù)


查看完整回答
反對(duì) 回復(fù) 2022-04-20
  • 2 回答
  • 0 關(guān)注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)