1つはurl
、query
パラメータを取得する
-
1.
url
タイプhttp:xxx:8080/person?name=hell&age=20
-
2.バックエンドでルーティングを定義します
func init() { beego.Router("/person", &person.PersonController{ }) }
-
3.バックエンドでパラメーターを取得します
package person import ( "fmt" "github.com/astaxie/beego" ) type PersonController struct { beego.Controller } //定义get请求 func (c *PersonController) Get() { //方式一 name1 := c.GetString("name") age1 := c.GetString("age") fmt.Println("方式一", name1, age1) //方式二 name2 := c.Input().Get("name") age2 := c.Input().Get("age") fmt.Println("方式二", name2, age2) c.TplName = "person.html" }
次に、パラメータを取得url
しparams
ます
-
1、ブラウザ
url
モードhttp:xx:8080/person/10
-
2.ルーティングを定義する場合
func init() { beego.Router("/", &controllers.MainController{ }) //后端通过接收id的方式来接收数据?:id:int也可以这样,那么只能接收一个int类型的数据 beego.Router("/person/?:id", &person.PersonController{ }) }
-
3.コントローラーでデータを受信します
:id
直接ではなく、****と書くことに注意してくださいid
//定义get请求 func (c *PersonController) Get() { //方式一 id1 := c.GetString(":id") fmt.Println("方式一", id1) //方式二 id3 := c.Ctx.Input.Param(":id") fmt.Println("方式三", id3) // 方式三,直接接收多个参数,一个map params := c.Ctx.Input.Params() c.TplName = "person.html" }
第三に、フロントエンドはpost
フォームを使用してデータを送信します
-
1.単純な
form
フォームを定義します<form action="/post" method="post"> <div> <label>用户名:</label> <input type="text" name="username"/> </div> <div> <label>密码:</label> <input type="password" name="password"> </div> <div> <button type="submit">提交</button> </div> </form>
-
2.ルーティングを定義します
func init() { // get请求会到get中,post请求会到post中 beego.Router("/post", &post.PostControllers{ }) }
-
3.コントローラーを定義します
package post import ( "fmt" "github.com/astaxie/beego" ) type PostControllers struct { beego.Controller } func (c *PostControllers) Get() { c.TplName = "post.html" } func (c *PostControllers) Post() { //方式一 username := c.GetString("username") password := c.GetString("password") fmt.Println("方式一获取参数", username, password) //方式二 username1 := c.Input().Get("username") password1 := c.Input().Get("password") fmt.Println("第二种方式", username1, password1) c.TplName = "test.tpl" }
第四に、post
提出されたデータは構造に解析されます
-
1.フロントエンドの静的コード
<form action="/post" method="post"> <div> <label>用户名:</label> <input type="text" name="username"/> </div> <div> <label>密码:</label> <input type="password" name="password"> </div> <div> <label>性别:</label> <label for="body"> <input type="radio" name="gender" value="1" id="body">男 </label> <label for="girl"> <input type="radio" name="gender" value="2" id="girl">女 </label> </div> <div> <label>价格:</label> <input type="text" name="price"> </div> <div> <label>是否记住密码</label> <input type="checkbox" name="isCheck"> </div> <div> <button type="submit">提交</button> </div> </form>
-
2.バックエンドは、データを受信するための構造を定義します
// 这里使用的是form表单接收的方式,就要定义form,方便前端传递过来的是username,在go语言中使用UserName type FormData struct { UserName string `form:"username"` Password string `form:"password"` Gender int `form:"gender"` Price float64 `form:"price"` IsCheck bool `form:"isCheck"` }
-
3.
post
でデータ要求を受信し、構造に保存しますfunc (c *PostControllers) Post() { // 定义一个结构体 formData := FormData{ } err := c.ParseForm(&formData) if err != nil { fmt.Println("解析错误") } fmt.Println(formData) c.TplName ="test.tpl" }
第五に、フロントエンドとバックエンドjson
でのデータ配信
-
1.構成ファイルで開きます
// conf/app.conf文件中加上这句 copyrequestbody = true
-
2.データを受信するための構造を定義します
//定义一个学生的结构体 type Student struct { Name string `json:"name"` Gender string `json:"gender"` Age int `json:"age"` }
-
3.定義された
post
コントローラーがデータを受信しますfunc (c *StudentControllers) Post() { var student Student data := c.Ctx.Input.RequestBody // 二进制的json数据 //将二进制的json解析到结构体中 err := json.Unmarshal(data, &student) if err != nil { fmt.Println("获取数据错误") } fmt.Println(student, "接收的数据") //注意这里必须返回一个map result := map[string]string{ "code": "200", "message": "成功"} c.Data["json"] = result //定义返回json c.ServeJSON() }
-
4.
postman
デバッグインターフェイスを使用します