簡單介紹一下Go中Gin使用get和post方法獲取前端資料
1.使用get方法獲取url中的引數
因為我使用的網頁只需要在url上傳一個引數,簡單介紹一下,get方法的傳參吧,可能不全,后續補充~
在主函式使用get/post方式加載需要從網頁中使用get/post方法獲取資料如下(默認使用get方法加載頁面)

對第三個使用get方法加載頁面,并使用get方法獲取URL中的引數,前端使用
傳參
網頁的url為:
后端獲取引數:addProof.GetRouteParams函式為:
func GetRouteParams(c *gin.Context) { name = c.Query("name") c.HTML(http.StatusOK, "insert.html", gin.H{ "Name": name, }) }
使用 name = c.Query("name")獲取URL的name引數,并將name傳遞給"insert.html"顯示在html上
insert.html部分代碼如下:
<body> <div class="topbar-wrap white"> <div class="topbar-inner clearfix"> <div class="topbar-logo-wrap clearfix"> <h1 class="topbar-logo none"><a href=https://www.cnblogs.com/echoqiqi/archive/2023/01/31/"index.html" class="navbar-brand">后臺管理</a></h1> <ul class="navbar-list clearfix"> <li><a class="on" href=https://www.cnblogs.com/echoqiqi/archive/2023/01/31/"index.html">首頁</a></li> <li>{{.Name}}</li> </ul> </div> <div class="top-info-wrap"> <ul class="top-info-list clearfix"> <li><a href=https://www.cnblogs.com/echoqiqi/archive/2023/01/31/"#">退出</a></li> </ul> </div> </div>
insert.html中{{.Name}}顯示為變數name的值,
頁面顯示為

2.使用Post方法獲取表單資料
Gin可以使用Post方法獲取前端的表單資料,需要宣告這個網頁可以使用post方式獲取前端資料,

login.html關于表單部分代碼:
<form action="" method="post" onsubmit="return check(this)"> <ul class="admin_items"> <li> <label for="username">用戶名:</label> <input type="text" name="username" id="username" size="35" class="admin_input_style" /> </li> <li> <label for="UserDepartment">部門:</label> <input type="text" name="UserDepartment" id="UserDepartment" size="35" class="admin_input_style" /> </li> <li> <input type="submit" tabindex="3" value=https://www.cnblogs.com/echoqiqi/archive/2023/01/31/"提交" name="sub" class="btn btn-primary" /> </li> </ul> </form>
網頁顯示為:

傳入資料:

接收表單資料的controller.PostM函式為:
func PostM(c *gin.Context) { //獲取引數 username := c.PostForm("username") UserDepartment := c.PostForm("UserDepartment") }
即可獲取前端資料,對其進行處理,
參考文獻:
http://wjhsh.net/zhouqi666-p-9808598.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/542699.html
標籤:其他
下一篇:初識Flask
