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

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

在 Go 問題中獲取 URL 參數(shù)

在 Go 問題中獲取 URL 參數(shù)

Go
HUX布斯 2021-10-25 18:34:13
我有一個看起來像這樣的 URL:http://localhost/templates/verify?key=ijio我的路由器看起來像這樣:import ("github.com/gorilla/mux""github.com/justinas/alice")ctx := &model.AppContext{db, cfg} // passes in database and configverifyUser := controller.Verify(ctx)mx.Handle("/verify", commonHandlers.ThenFunc(verifyUser)).Methods("GET").Name("verify")我想從 URL 中獲取關(guān)鍵參數(shù),所以我使用以下代碼:func Verify(c *model.AppContext) http.HandlerFunc {    fn := func(w http.ResponseWriter, r *http.Request) {    key := r.URL.Query().Get("key") // gets the hash value that was placed in the URL    log.Println(key) // empty key    log.Println(r.URL.Query()) // returns map[]    // code that does something with key and sends back JSON response   }}我使用 AngularJS 來獲取 JSON 數(shù)據(jù):app.controller("verifyControl", ['$scope', '$http', function($scope, $http) {    $scope.message = "";    $http({      method: 'GET',      url: "/verify"    }).success(function(data) {         $scope.message = data.msg; // JSON response    });  }]);但是,當(dāng)我嘗試打印它時,我最終得到了一個空的鍵變量。我最近使用 nginx 刪除了我的 .html 擴展名,如果這可能是導(dǎo)致此問題的原因。我該如何解決?
查看完整描述

1 回答

?
慕哥9229398

TA貢獻(xiàn)1877條經(jīng)驗 獲得超6個贊

我的問題的解決方案涉及通過以下方式檢查請求 URL 鏈接


log.Print(r.URL) // This returns "/verify"

然而,這并不完全是你想要的。相反,您需要完整的 URL。您可以執(zhí)行以下操作以獲取完整 URL 并從中提取參數(shù):


urlStr := r.Referer()             // gets the full URL as a string

urlFull, err := url.Parse(urlStr) // returns a *URL object

if err != nil {

    log.Fatal(err)

    return

}

key := urlFull.Query().Get("key") // now we get the key parameter from the URL

log.Println("Key: " + key) // now you'll get a non empty string


查看完整回答
反對 回復(fù) 2021-10-25
  • 1 回答
  • 0 關(guān)注
  • 455 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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