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>
---童飞