设为首页 加入收藏

TOP

Gin-Go学习笔记六:Gin-Web框架 Api的编写(二)
2017-12-23 06:06:46 】 浏览:3415
Tags:Gin-Go 学习 笔记 Gin-Web 框架 Api 编写
lt;/div> <!--请求得到Json数据--> <div style="width:100%;height:50px;"> <button onclick="getjson()" class="btn btn-primary">得到Json</button> <label id="txtjson"></label> </div> <!--请求得到Xml数据--> <div style="width:100%;height:50px;"> <button onclick="getxml()" class="btn btn-primary">得到Xml</button> <label id="txtxml"></label> </div> <!--请求得到Yaml数据--> <div style="width:100%;height:50px;"> <button onclick="getYaml()" class="btn btn-primary">得到Yaml</button> <label id="txtyaml"></label> </div> </div> <!--JS部分--> <script type="text/java script"> //得到参数 function getparams(){ $.ajax({ type:'get', url:'/api/paramsdata',//此处的是json数据的格式 data:{ search:$("#search").val() }, success:function(result){ console.log('获取参数的数据') console.log(result) $("#txtparams").html("记录总数:"+result.count +",记录I:"+result.datalist[0].first_name+result.datalist[0].last_name+",记录II:" +result.datalist[1].first_name+result.datalist[1].last_name+"..."); } }) } //得到Json function getjson(){ $.ajax({ type:'get', url:'/api/jsondata', dataType:'json',//此处的是json数据的格式 data:{ search:$("#search").val() }, success:function(result){ console.log('获取json的数据') console.log(result) $("#txtjson").html("json的结果:"+result.count +",记录1:"+result.datalist[0].first_name+result.datalist[0].last_name+",记录2:" +result.datalist[1].first_name+result.datalist[1].last_name+"..."); } }) } //得到Xml function getxml(){ $.ajax({ type:'get', url:'/api/xmldata', dataType:'xml',//此处的是xml数据的格式 data:{ search:$("#search").val() }, success:function(result){ console.log('获取xml的数据') console.log(result) $("#txtxml").html("xml的结果:"+$(result).text()); } }) } //得到yaml function getYaml(){ $.ajax({ type:'get', url:'/api/yamldata', data:{ search:$("#search").val() }, success:function(result){ console.log('获取yaml的数据') console.log(result) $("#txtyaml").html("yaml的结果:"+result); } }) } </script>     </body> </html>

  

4>     在路由器文件router.go中添加api部分的路由。具体代码如下:

    

package routers

import (
  "github.com/gin-gonic/gin"
  . "GinLearn/GinLearn/apis" //api部分
  . "GinLearn/GinLearn/controllers" //constroller部分
 )
 
func InitRouter() *gin.Engine{
  router := gin.Default()
  //Hello World
  router.GET("/", IndexApi)
  //渲染html页面
  router.LoadHTMLGlob("views/*")
  router.GET("/home/index", ShowHtmlPage)
  //列表页面
  router.GET("/home/list", ListHtml)
  router.POST("/home/PageData", GetDataList)
  router.POST("/home/PageNextData", PageNextData)

  //新增页面
  router.GET("/home/add", AddHtml)
  router.POST("/ho
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Gin-Go学习笔记四:Gin-Web框架 .. 下一篇Gin-Go学习笔记五:Gin-Web框架 ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目