美文网首页
WebForm实现Ajax

WebForm实现Ajax

作者: 蚁丶点 | 来源:发表于2018-07-25 17:34 被阅读0次

前端JS代码

        $(document).ready(function () {

            $("#in").click(function () {//返回表格數據

var dateF = $("#ApplyDateFrom").val();

                $.ajax({

                    type: "POST",

                    url: "SurpportDemandManage.aspx/GetUserData",

                    data: "{ dateFrom:'" + dateF + "'}",

                    contentType: "application/json; charset=utf-8",

                    success: function (dt) {

                        var obj = $.parseJSON(dt.d); //这个数据

                        var name = obj[0].SUBJECT;

                        alert(name);

                        $('#dg').datagrid('loadData', obj);

                    },

                    error: function () {

                        alert('error');

                    },

                    failure: function () {

                        alert('failure');

                    }

                });

            });

            $("#Button1").click(function () {//返回單純字符串

                $.ajax({

                    type: "POST",

                    url: "SurpportDemandManage.aspx/GetUser",

                    contentType: "application/json; charset=utf-8",

                    success: function (dt) {

                        alert(11);

                        alert(eval(dt.d));

                    },

                    error: function () {

                        alert('error');

                    },

                    failure: function () {

                        alert('failure');

                    }

                });

            })

        })

后台cs代码

[WebMethod]

        public static string GetUserData(string dateFrom)

        {

            string strSP = "hrm_support.surpport_demand_manage_pk.get_surpport_demand_sps";

            string strFactoryF = "";

            string strFactoryT = "";

            string strDateF = "2018-05-25";

            string strDateT = "2018-07-25";

            string[][] strArry = new string[][] {

                new string[]{"v_supported_site","10",strFactoryF},

                new string[]{"v_be_supported_site","10",strFactoryT},

                new string[]{"V_USER_NO","10",""},

                new string[]{"v_begin_date","20",strDateF},

                new string[]{"v_end_date","20",strDateT}

            };

            string res = "";

            DataTable dt = cd.GetData(strArry, strSP, out res);

            string strJson = JsonConvert.SerializeObject(dt);//直接将DataTable转json

            return strJson;

        }

        [WebMethod]

        public static string GetUser()

        {

            return "US";

        }

注意点:

1.后台方法必须加[WebMethod]注释

2.后台方法必须是 public static

3.ajax请求必须是POST

4.ajax需加上contentType: "application/json; charset=utf-8",

5.取得后台返回数据需加上“.d”(害我调半天总取不到数据,后来用浏览器调试才发现被放在“d”里面了)

相关文章

网友评论

      本文标题:WebForm实现Ajax

      本文链接:https://www.haomeiwen.com/subject/kphmmftx.html