. 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         }

좋은 웹페이지 즐겨찾기