.Net中jQuery.ajax()调用asp.net后台方法 总结

利用JQuery的$.ajax()调用.Net后台方法有多种方式,

不多说了  直接上代码

前台代码

 <script type="text/javascript">        

         $(document).ready(function () {
getTreeData(getData());
}); //获取ajax调用传参方法
function getData() {
var data = {};
data.username = "an";
data.id = "2";
return data;
} var getTreeData = function (data) {
$.ajax({
//界面URL传参数调用
url: "Edit_super.aspx?method=GetNodeData&dataId=1",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
//Response.Write 方式返回数据
success: function (dataReturn) {
$.fn.zTree.init($("#treeDemo"), setting, dataReturn);
$("#selectAll").bind("click", selectAll);
},
error: function () {
}
});
}; var getTreeData = function (data) {
$.ajax({
// 调用后台方法方式
url: "Edit_super.aspx/GetValueAjax",
//传入参数
data: "{id:'" + data.id + "'}",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
//解析返回的json数据
success: function (dataReturn) {
var datar=$.parseJSON(dataReturn.d);
$.fn.zTree.init($("#treeDemo"), setting, datar);
$("#selectAll").bind("click", selectAll);
},
error: function () {
}
});
}; var getTreeData = function (data) {
//MVC 调用方式
$.ajax({
url: '../GetValue/GetValueAjax',
type: "POST",
data: data,
success: function (dataReturn) {
//解析返回数据
var ss = $.parseJSON(dataReturn);
},
error: function () {
}
});
};
</script>

后台代码

   protected void Page_Load(object sender, EventArgs e)
{
switch (Request["method"])
{
case "GetNodeData": GetNodeData(); break;
}
} /// <summary>
/// 界面URL传参数调用
/// </summary>
public void GetNodeData( )
{
string dataId = Request["dataId"]; List<JosnModel> josnModels = new List<JosnModel>(); JosnModel model1 = new JosnModel() { id = , name = "父节点1", open = true, pId = , isLastNode = false };
JosnModel model2 = new JosnModel() { id = , name = "父节点1-1", pId = , isLastNode = false };
JosnModel model3 = new JosnModel() { id = , name = "父节点1-2", pId = , isLastNode = false };
JosnModel model4 = new JosnModel() { id = , name = "父节点1-3", pId = , isLastNode = false };
JosnModel model5 = new JosnModel() { id = , name = "父节点2", open = true, pId = , isLastNode = false };
JosnModel model6 = new JosnModel() { id = , name = "父节点2-1", pId = , isLastNode = false };
JosnModel model7 = new JosnModel() { id = , name = "父节点3", open = true, pId = , isLastNode = false };
JosnModel model8 = new JosnModel() { id = , name = "父节点3-1", pId = , isLastNode = false };
josnModels.Add(model1);
josnModels.Add(model2);
josnModels.Add(model3);
josnModels.Add(model4);
josnModels.Add(model5);
josnModels.Add(model6);
josnModels.Add(model7);
josnModels.Add(model8); string nodeData = new JavaScriptSerializer().Serialize(josnModels); Response.Clear();
Response.Write(nodeData);
Response.End();
} /// <summary>
/// 调用后台方法方式
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[WebMethod] // 静态方法 并且要有[WebMethod] 标志
public static string GetValueAjax(string id)
{
string dataId = id; List<JosnModel> josnModels = new List<JosnModel>(); JosnModel model1 = new JosnModel() { id = , name = "父节点1", open = true, pId = , isLastNode = false };
JosnModel model2 = new JosnModel() { id = , name = "父节点1-1", pId = , isLastNode = false };
JosnModel model3 = new JosnModel() { id = , name = "父节点1-2", pId = , isLastNode = false };
JosnModel model4 = new JosnModel() { id = , name = "父节点1-3", pId = , isLastNode = false };
JosnModel model5 = new JosnModel() { id = , name = "父节点2", open = true, pId = , isLastNode = false };
JosnModel model6 = new JosnModel() { id = , name = "父节点2-1", pId = , isLastNode = false };
JosnModel model7 = new JosnModel() { id = , name = "父节点3", open = true, pId = , isLastNode = false };
JosnModel model8 = new JosnModel() { id = , name = "父节点3-1", pId = , isLastNode = false };
josnModels.Add(model1);
josnModels.Add(model2);
josnModels.Add(model3);
josnModels.Add(model4);
josnModels.Add(model5);
josnModels.Add(model6);
josnModels.Add(model7);
josnModels.Add(model8); string nodeData = new JavaScriptSerializer().Serialize(josnModels);
return nodeData;
} /// <summary>
/// MVC调用方法
/// </summary>
/// <returns></returns>
public string GetValueAjax()
{
//获取传入的参数
string nodeId= Request.Form["id"].ToString( ); List<JosnModel> josnModels = new List<JosnModel>();
JosnModel model1 = new JosnModel() { id = , name = "父节点1", open = true, pId = , isLastNode = false };
JosnModel model2 = new JosnModel() { id = , name = "父节点1-1", pId = , isLastNode = false };
JosnModel model3 = new JosnModel() { id = , name = "父节点1-2", pId = , isLastNode = false };
JosnModel model4 = new JosnModel() { id = , name = "父节点1-3", pId = , isLastNode = false };
JosnModel model5 = new JosnModel() { id = , name = "父节点2", open = true, pId = , isLastNode = false };
JosnModel model6 = new JosnModel() { id = , name = "父节点2-1", pId = , isLastNode = false };
JosnModel model7 = new JosnModel() { id = , name = "父节点3", open = true, pId = , isLastNode = false };
JosnModel model8 = new JosnModel() { id = , name = "父节点3-1", pId = , isLastNode = false };
josnModels.Add(model1);
josnModels.Add(model2);
josnModels.Add(model3);
josnModels.Add(model4);
josnModels.Add(model5);
josnModels.Add(model6);
josnModels.Add(model7);
josnModels.Add(model8); string nodeData = new JavaScriptSerializer().Serialize(josnModels);
return nodeData;
}
上一篇:详解JQuery Ajax 在asp.net中使用总结


下一篇:英语初级学习系列-00-Hello-打招呼