.Net中jQuery.ajax()调用asp.net后台方法 总结
利用JQuery的$.ajax()调用.Net后台方法有多种方式,
不多说了 直接上代码
前台代码
1 <script type="text/javascript"> 2 3 $(document).ready(function () { 4 getTreeData(getData()); 5 }); 6 7 //获取ajax调用传参方法 8 function getData() { 9 var data = {}; 10 data.username = "an"; 11 data.id = "2"; 12 return data; 13 } 14 15 var getTreeData = function (data) { 16 $.ajax({ 17 //界面URL传参数调用 18 url: "Edit_super.aspx?method=GetNodeData&dataId=1", 19 type: "POST", 20 contentType: "application/json; charset=utf-8", 21 dataType: "json", 22 //Response.Write 方式返回数据 23 success: function (dataReturn) { 24 $.fn.zTree.init($("#treeDemo"), setting, dataReturn); 25 $("#selectAll").bind("click", selectAll); 26 }, 27 error: function () { 28 } 29 }); 30 }; 31 32 var getTreeData = function (data) { 33 $.ajax({ 34 // 调用后台方法方式 35 url: "Edit_super.aspx/GetValueAjax", 36 //传入参数 37 data: "{id:'" + data.id + "'}", 38 type: "POST", 39 contentType: "application/json; charset=utf-8", 40 dataType: "json", 41 //解析返回的json数据 42 success: function (dataReturn) { 43 var datar=$.parseJSON(dataReturn.d); 44 $.fn.zTree.init($("#treeDemo"), setting, datar); 45 $("#selectAll").bind("click", selectAll); 46 }, 47 error: function () { 48 } 49 }); 50 }; 51 52 var getTreeData = function (data) { 53 //MVC 调用方式 54 $.ajax({ 55 url: '../GetValue/GetValueAjax', 56 type: "POST", 57 data: data, 58 success: function (dataReturn) { 59 //解析返回数据 60 var ss = $.parseJSON(dataReturn); 61 }, 62 error: function () { 63 } 64 }); 65 }; 66 </script>
后台代码
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 switch (Request["method"]) 4 { 5 case "GetNodeData": GetNodeData(); break; 6 } 7 } 8 9 /// <summary> 10 /// 界面URL传参数调用 11 /// </summary> 12 public void GetNodeData( ) 13 { 14 string dataId = Request["dataId"]; 15 16 List<JosnModel> josnModels = new List<JosnModel>(); 17 18 JosnModel model1 = new JosnModel() { id = 1, name = "父节点1", open = true, pId = 0, isLastNode = false }; 19 JosnModel model2 = new JosnModel() { id = 11, name = "父节点1-1", pId = 1, isLastNode = false }; 20 JosnModel model3 = new JosnModel() { id = 12, name = "父节点1-2", pId = 1, isLastNode = false }; 21 JosnModel model4 = new JosnModel() { id = 13, name = "父节点1-3", pId = 1, isLastNode = false }; 22 JosnModel model5 = new JosnModel() { id = 2, name = "父节点2", open = true, pId = 0, isLastNode = false }; 23 JosnModel model6 = new JosnModel() { id = 21, name = "父节点2-1", pId = 2, isLastNode = false }; 24 JosnModel model7 = new JosnModel() { id = 3, name = "父节点3", open = true, pId = 0, isLastNode = false }; 25 JosnModel model8 = new JosnModel() { id = 31, name = "父节点3-1", pId = 3, isLastNode = false }; 26 josnModels.Add(model1); 27 josnModels.Add(model2); 28 josnModels.Add(model3); 29 josnModels.Add(model4); 30 josnModels.Add(model5); 31 josnModels.Add(model6); 32 josnModels.Add(model7); 33 josnModels.Add(model8); 34 35 string nodeData = new JavaScriptSerializer().Serialize(josnModels); 36 37 Response.Clear(); 38 Response.Write(nodeData); 39 Response.End(); 40 } 41 42 /// <summary> 43 /// 调用后台方法方式 44 /// </summary> 45 /// <param name="id"></param> 46 /// <returns></returns> 47 [WebMethod] // 静态方法 并且要有[WebMethod] 标志 48 public static string GetValueAjax(string id) 49 { 50 string dataId = id; 51 52 List<JosnModel> josnModels = new List<JosnModel>(); 53 54 JosnModel model1 = new JosnModel() { id = 1, name = "父节点1", open = true, pId = 0, isLastNode = false }; 55 JosnModel model2 = new JosnModel() { id = 11, name = "父节点1-1", pId = 1, isLastNode = false }; 56 JosnModel model3 = new JosnModel() { id = 12, name = "父节点1-2", pId = 1, isLastNode = false }; 57 JosnModel model4 = new JosnModel() { id = 13, name = "父节点1-3", pId = 1, isLastNode = false }; 58 JosnModel model5 = new JosnModel() { id = 2, name = "父节点2", open = true, pId = 0, isLastNode = false }; 59 JosnModel model6 = new JosnModel() { id = 21, name = "父节点2-1", pId = 2, isLastNode = false }; 60 JosnModel model7 = new JosnModel() { id = 3, name = "父节点3", open = true, pId = 0, isLastNode = false }; 61 JosnModel model8 = new JosnModel() { id = 31, name = "父节点3-1", pId = 3, isLastNode = false }; 62 josnModels.Add(model1); 63 josnModels.Add(model2); 64 josnModels.Add(model3); 65 josnModels.Add(model4); 66 josnModels.Add(model5); 67 josnModels.Add(model6); 68 josnModels.Add(model7); 69 josnModels.Add(model8); 70 71 string nodeData = new JavaScriptSerializer().Serialize(josnModels); 72 return nodeData; 73 } 74 75 76 /// <summary> 77 /// MVC调用方法 78 /// </summary> 79 /// <returns></returns> 80 public string GetValueAjax() 81 { 82 //获取传入的参数 83 string nodeId= Request.Form["id"].ToString( ); 84 85 List<JosnModel> josnModels = new List<JosnModel>(); 86 JosnModel model1 = new JosnModel() { id = 1, name = "父节点1", open = true, pId = 0, isLastNode = false }; 87 JosnModel model2 = new JosnModel() { id = 11, name = "父节点1-1", pId = 1, isLastNode = false }; 88 JosnModel model3 = new JosnModel() { id = 12, name = "父节点1-2", pId = 1, isLastNode = false }; 89 JosnModel model4 = new JosnModel() { id = 13, name = "父节点1-3", pId = 1, isLastNode = false }; 90 JosnModel model5 = new JosnModel() { id = 2, name = "父节点2", open = true, pId = 0, isLastNode = false }; 91 JosnModel model6 = new JosnModel() { id = 21, name = "父节点2-1", pId = 2, isLastNode = false }; 92 JosnModel model7 = new JosnModel() { id = 3, name = "父节点3", open = true, pId = 0, isLastNode = false }; 93 JosnModel model8 = new JosnModel() { id = 31, name = "父节点3-1", pId = 3, isLastNode = false }; 94 josnModels.Add(model1); 95 josnModels.Add(model2); 96 josnModels.Add(model3); 97 josnModels.Add(model4); 98 josnModels.Add(model5); 99 josnModels.Add(model6); 100 josnModels.Add(model7); 101 josnModels.Add(model8); 102 103 string nodeData = new JavaScriptSerializer().Serialize(josnModels); 104 return nodeData; 105 }