Uno, obtén url
los query
parámetros
-
1.
url
Tipohttp:xxx:8080/person?name=hell&age=20
-
2. Defina el enrutamiento en el backend
func init() { beego.Router("/person", &person.PersonController{ }) }
-
3. Obtenga parámetros en el 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" }
En segundo lugar, obtenga url
los params
parámetros
-
1, el
url
modo de navegadorhttp:xx:8080/person/10
-
2. Al definir el enrutamiento
func init() { beego.Router("/", &controllers.MainController{ }) //后端通过接收id的方式来接收数据?:id:int也可以这样,那么只能接收一个int类型的数据 beego.Router("/person/?:id", &person.PersonController{ }) }
-
3. Recibir datos sobre el responsable del tratamiento
Nota para escribir **
:id
**, no directamenteid
//定义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" }
En tercer lugar, la interfaz utiliza el post
formulario para enviar datos.
-
1. Defina una
form
forma simple<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. Definir enrutamiento
func init() { // get请求会到get中,post请求会到post中 beego.Router("/post", &post.PostControllers{ }) }
-
3. Defina el controlador
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" }
Cuarto, los post
datos enviados se analizan en la estructura
-
1. Código estático de front-end
<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. El back-end define una estructura para recibir datos.
// 这里使用的是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
Recibir la solicitud de datos y almacenarla en la estructurafunc (c *PostControllers) Post() { // 定义一个结构体 formData := FormData{ } err := c.ParseForm(&formData) if err != nil { fmt.Println("解析错误") } fmt.Println(formData) c.TplName ="test.tpl" }
En quinto lugar, json
la entrega de datos al principio y al final
-
1. Abrir en el archivo de configuración
// conf/app.conf文件中加上这句 copyrequestbody = true
-
2. Defina una estructura para recibir datos
//定义一个学生的结构体 type Student struct { Name string `json:"name"` Gender string `json:"gender"` Age int `json:"age"` }
-
3. El
post
controlador definido recibe datosfunc (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. Utilice la
postman
interfaz de depuración