语法: $(function(){ var callback = function(r){ //r表示后台数据返回的数据. } $.get("",callback); //引号里面写方法的路径 });
这样就可以得到后台方法得到的数据显示在html前端了.
以下是工作中写的一段js和后台方法:
js:
$(function(){
var callback = function (r) {
var data = "
" var data2 = "优惠名单
"; for (var i = 0; i < r.length; i ) { data = ("" r[i].time " " r[i].CustomerName " 申请试用成功获得购买年服务年费9折优惠,恭喜! ");
} data = "
";
$("#01_companys").html(data);
var t_data2 = "";
for (var i = 0; i < r.length; i ) {
if (i % 2 == 0) {
t_data2 = "
";
}
t_data2 = "
" (i 1) "." r[r.length - i - 1].CustomerName "
";
}
$("#01_list").html(data2 (t_data2.length > 0 ? t_data2.substr(6, t_data2.length - 6) : "") "
");
}
$.get("/activity/trycompany", callback);
});
public ActionResult TryCompany()
{
string CustomerType = Res.CustomerType.TestCustomer.ToString();
IList
vlist = o_Res.GetHtmlCustomers(1, 100, out pCount, (a => a.CustomerType == CustomerType));
var list = vlist.ToList().ConvertAll(s => { return new { s.CustomerName, time = s.AddDate.HasValue ? s.AddDate.Value.ToString("MM-dd HH:mm") : "" }; }); return Json(list, JsonRequestBehavior.AllowGet); }
后台得到一个list,jq调用,遍历,然后显示在前端.
一个很简单的例子,肯定还有更简单的办法,等以后来挖掘.