在js 中使用ajax 调用后台代码方法,解析返回值

时间:2021-08-31 23:56:55

 一 . 在js 中使用ajax  调用后台代码方法,解析返回值

 

 

1 ajax 请求格式

 

  $.ajax({

            type: "Post",

            url: "/coupons.aspx/AllVouchers",

            contentType: "application/json",

            data: "{'index': '" + index + "', 'status': '" + status + "'}",

            dataType: 'json',

        success: function (data) {

            data = JSON.parse(data.d);  

           }

});

 

$.ajax({

        type: "Post",

        url: "/coupons.aspx/AllVouchersCount",

        contentType: "application/json",

        data: "{ 'status': '" + status + "'}",

        dataType: 'json',

        success: function (data) {

          

            data= eval("(" + data.d + ")").result;

           }

});

 

 

2  后台方法

//列表

  public partial class coupons : System.Web.UI.Page

 {

        [WebMethod]

        public static string AllVouchers(string index,string status )

        {

            int pageindex = Convert.ToInt32(index);

            int page = 9;

 

            string uid = "";

            if (HttpContext.Current.Session["uid"] == null)

            {

                HttpContext.Current.Response.Redirect("login.aspx");

            }

            else

            {

                uid = PubEncryptionFunc.PublicDecrypt(HttpContext.Current.Session["uid"].ToString());

            }

 

            return JsonConvert.SerializeObject(COUPON_DAIJIN.getInstanct().DsCoupon_DaiJin(uid, status, pageindex, page));

        }

 

 

    //总计录数

        [WebMethod]

        public static  string AllVouchersCount(string status)

        {

            string uid = "";

            if (HttpContext.Current.Session["uid"] == null)

            {

                HttpContext.Current.Response.Redirect("login.aspx");

            }

            else

            {

                uid = PubEncryptionFunc.PublicDecrypt(HttpContext.Current.Session["uid"].ToString());

            }

           string json = "{\"result\":\""+ COUPON_DAIJIN.getInstanct().Coupon_DaiJinCount(uid, status).ToString() + "\"}";

            return json ;

        }