前端ajax获取接口,后端与前端交互Ajax接口(示例代码)

这种交互方式运用MVC的设计模式,有一个简单的交互。

这是一种读取数据并分页。将所获取对象转换成Json格式

后台controller Ajax接口:

public ActionResult  视图名称 (int p=1,int s=1,string keyword="")

{

JsonpResult json = CommonBLL.GetErrorJSONP("初始化中");

try

{

var pm = new PageModel()

{

CurrentPage=p,

PageSize=s,

};

if (keyword != "")

{

pm.OrCondition.Add(new SearchCondition()

{

ConditionField="字段名称",

SearchType=SearchType.Like,

ConditionValue1=keyword,

});

}

var LstClass = ClassBLL.GetClass(pm);

var getval = new

{

success=true,

code=0,

data=LstClass,

datacount=pm.DataCount,

maxpage=pm.MaxPage,

};

json = new JsonpResult(getval);

}

catch(Exception ex)

{

json = CommonBLL.GetErrorJSONP(ex.Message);

}

return json;

}

Ajax:

$.ajax({

type: "POST",

url: gSiteURI + "FrontEnd/GetClassList",

data: "p=" + pm.CurrentPage +

"&s=" + pm.PageSize +

"&keyword=" + $("#keyword").val(),

dataType: "json",

success: function (jsonObj) {

if (jsonObj.success) {

pm.DataCount = jsonObj.datacount;

pm.MaxPage = jsonObj.maxpage;

pm.PageEventCallBack = function () { LoadData(); };

ModelPagerInit("Data_pager", "Data");

var sList = jsonObj.data;

var htmlStr = "";

for (var i = 0; i < sList.length; i++) {

var data = sList[i];

htmlStr += ‘

‘;

htmlStr += ‘

‘;

htmlStr += ‘

‘ + data.ClassName + ‘‘;

htmlStr += ‘

‘;

htmlStr += ‘‘;

htmlStr += ‘‘;

htmlStr += ‘‘;

htmlStr += ‘‘;

htmlStr += ‘‘;

htmlStr += ‘‘;

htmlStr += ‘

‘;

htmlStr += ‘

‘;

}

htmlStr = htmlStr !== "" ? htmlStr : "

暂无数据";

$("#Data_tbody").html(htmlStr);

}

else {

alert(jsonObj.msg);

}

HideLoadingUI();

},

error: function (XMLHttpRequest, textStatus, errorThrown) {

alert(errorThrown);

HideLoadingUI();

}

});

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值