24
2016
03

C# ajax 前后台写法

后台

  1. 使用[WebMethod]标注

  2. 使用静态方法 static

       [WebMethod]

        public static string GetCustomName(string CustomerID)

        {

            int m_CustomerID = 0;

            int.TryParse(CustomerID, out m_CustomerID);

            var customer = CustomerInfoBll.GetModel(m_CustomerID);

            if (customer != null)

            {            

                return customer.Name;

            }

            return "--";

        }

--------------------------------------------------------------------

前台

 function ShowCustormerName(CustomerID) {

            $.ajax({

                type: "post",

                url: "ViewMonitor.aspx/GetCustomName",  //后台方法路径及方法名称

                data: "{'CustomerID':'" + CustomerID + "'}",   //传入参数

                contentType: "application/json;charset=utf-8",// 这句可不要忘了。

                dataType: "json",

                success: function (res) {

//成功时的操作

                    $("#NameDiv").html(res.d);//Jquery 返回的有效数据存储与 d 属性中  ( 未查明原因 )

 //var m_Data = eval("(" + res.d + ")"); //如果返回的为符合json 格式的字符串,可以使用eval("("++")")方法解析其中的内容,形成json对象

                },

                error: function (xmlReq, err, c) {

//失败时的操作

                    //$("#dataShow").text("error:" + err);

                }

            });

        }


« 上一篇下一篇 »

相关文章:

c# 使用ping 方法  (2015-12-17 11:37:39)

豫ICP备13016324号

mdeveloper