我有一個看起來像這樣的 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
- 1 回答
- 0 關(guān)注
- 455 瀏覽
添加回答
舉報
0/150
提交
取消