beego實現web api接口


1)程序代碼:

/**
 * 類似beego版物聯網首頁產品數據的調用
 */import (
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/httplib"
)

type MainController struct {
    beego.Controller
}

func (this *MainController) Get() {

    str, _ := httplib.Get("http://shop.iotywl.com/tools/cms.ashx?cmdParam=Select_Product_List").String()

    this.Data["JsonDatas"] = str

    this.TplNames = "index.html"
}

2)視圖代碼:

<!DOCTYPE html>

<html>
    <head>
        <title>Beego</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script type="text/javascript" src="/static/js/jquery-1.9.1.min.js"></script>
    </head>
    <body>
          <ul id="html_data"></ul>
    </body>
    <script type="text/javascript">
      var jsonDatas= "{{.JsonDatas}}";
  $(
function(){ var web_url = "http://shop.iotywl.com"; var image_url = "http://images.iotywl.com" var list=eval(jsonDatas); var strTemp = "";
for (var i = 0; i < list.length; i++) { strTemp += "<li><a href=\"" + web_url + "/Product/ProductDetail/" + list[i].Id + "\" target=\"_blank\"><img src=\"" + image_url + list[i].SmallImage + "\" /><p>" + list[i].Title + "</p></a></li>"; } $("#html_data").html(strTemp); })   </script> </html>

 

   ---童飛


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM