go框架bee先後端數據交互的幾種方式

1、獲取url上的query參數

  • 一、url類型html

    http:xxx:8080/person?name=hell&age=20
  • 二、後端中定義路由前端

    func init() {
        beego.Router("/person", &person.PersonController{})
    }
  • 三、在後端中獲取參數git

    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"
    }

2、獲取url上的params參數

  • 一、瀏覽器上url方式github

    http:xx:8080/person/10
  • 二、定義路由的時候shell

    func init() {
        beego.Router("/", &controllers.MainController{})
        //後端經過接收id的方式來接收數據?:id:int也能夠這樣,那麼只能接收一個int類型的數據
        beego.Router("/person/?:id", &person.PersonController{})
    }
  • 三、在控制器上接收數據json

    注意要寫**: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"
    }

3、前端使用post表單提交數據

  • 一、定義一個簡單的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>
  • 二、定義路由markdown

    func init() {
      	// get請求會到get中,post請求會到post中
        beego.Router("/post", &post.PostControllers{})
    }
  • 三、定義控制器app

    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"
    }

4、post提交數據解析到結構體

  • 一、前端靜態代碼

    <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>
  • 二、後端定義一個接收數據的結構體

    // 這裏使用的是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"`
    }
  • 三、在post中接收數據請求,而後存儲到結構體中

    func (c *PostControllers) Post() {
    	// 定義一個結構體
    	formData := FormData{}
    	err := c.ParseForm(&formData)
    	if err != nil {
    		fmt.Println("解析錯誤")
    	}
    	fmt.Println(formData)
    	c.TplName ="test.tpl"
    }

5、先後端進行json數據交付

  • 一、要在配置文件中開啓

    // conf/app.conf文件中加上這句
    copyrequestbody = true
  • 二、定義一個結構體用來接收數據的

    //定義一個學生的結構體
    type Student struct {
    	Name   string `json:"name"`
    	Gender string `json:"gender"`
    	Age    int    `json:"age"`
    }
  • 三、定義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()
    }
  • 四、用postman調試接口

相關文章
相關標籤/搜索