go框架bee前后端数据交互的几种方式

一、获取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调试接口

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水痕01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值