One, get url
the query
parameters
-
1.
url
Typehttp:xxx:8080/person?name=hell&age=20
-
2. Define routing in the backend
func init() { beego.Router("/person", &person.PersonController{ }) }
-
3. Get parameters in the backend
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" }
Second, get url
the params
parameters
-
1, the browser
url
modehttp:xx:8080/person/10
-
2. When defining routing
func init() { beego.Router("/", &controllers.MainController{ }) //后端通过接收id的方式来接收数据?:id:int也可以这样,那么只能接收一个int类型的数据 beego.Router("/person/?:id", &person.PersonController{ }) }
-
3. Receive data on the controller
Note to write **
:id
**, not directlyid
//定义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" }
Third, the front end uses the post
form to submit data
-
1. Define a simple
form
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. Define routing
func init() { // get请求会到get中,post请求会到post中 beego.Router("/post", &post.PostControllers{ }) }
-
3. Define the controller
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" }
Fourth, the post
submitted data is parsed into the structure
-
1. Front-end static code
<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. The back end defines a structure to receive data
// 这里使用的是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
Receive the data request in and store it in the structurefunc (c *PostControllers) Post() { // 定义一个结构体 formData := FormData{ } err := c.ParseForm(&formData) if err != nil { fmt.Println("解析错误") } fmt.Println(formData) c.TplName ="test.tpl" }
Fifth, json
data delivery at the front and back ends
-
1. Open in the configuration file
// conf/app.conf文件中加上这句 copyrequestbody = true
-
2. Define a structure to receive data
//定义一个学生的结构体 type Student struct { Name string `json:"name"` Gender string `json:"gender"` Age int `json:"age"` }
-
3. The defined
post
controller receives datafunc (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. Use the
postman
debug interface