Ajax嵌套调用WebApi接口和后台方法(传参)

1、 按钮控件

 <asp:TemplateColumn HeaderText="信息推送" SortExpression="message_push">
                        <ItemTemplate>
                            <asp:button id="Messagepush" runat="server" text="推送" cssclass="BtnList" onclick="MessagePush_Click" commandname='<%# DataBinder.Eval(Container, "DataItem.RowGuid") %>'>asp:button>
                        ItemTemplate>
                    asp:TemplateColumn>

2、 后台事件方法

引用:
using System.Web.Services;

  protected void MessagePush_Click(object sender, System.EventArgs e)
        {
            string RowGuid = ((RongGuang.Web.UI.WebControls2X.Button)sender).CommandName;
            MisGuidRow Mis = new MisGuidRow("InfoPushManager");
            DataView Dv = Mis.Select("*", " RowGuid='" + RowGuid + "'");
            string TitlePush = Convert.ToString(Dv[0]["Info_Title"]);
            string ContentPush = Convert.ToString(Dv[0]["Info_Content"]);           
            Post(TitlePush, ContentPush, RowGuid);
        }
     protected void Post(string Title, string Content, string RowGuid)
        {
            MessagePush MP = new MessagePush();
            MP.Header = "Push";
            MP.Flag = "none";
            string[] tempAdditional = new string[0];
            MP.Additional = tempAdditional;
            List body = new List();
            Body itembody = new Body();
            itembody.Alert = Content;

            //更改后代码,取ACcountsID作为发送对象
            DataView Dv = DB.ExecuteDataView("select ACcountsID from MG_CCounts where IsGroup='0' and GroupRowGuid='0000' and IsBind='1'");

            string[] temp = new string[Dv.Count];

            for (int i = 0; i < Dv.Count; i++)
            {
                temp[i] = Convert.ToString(Dv[i][0]);
            }
            itembody.Alias = temp;
            itembody.Title = Title;
            string[] Extras = new string[0];
            itembody.Extras = Extras;
            string[] tempTags = new string[0];
            body.Add(itembody);
            MP.Body = body;
            string str = string.Join(",", temp);
            string _json = JsonConvert.SerializeObject(MP);
            //   string _json = "[{\"Header\":\"Push\",\"Flag\":\"none\",\"Body\":[{\"Alert\":\"我是通知内容\",\"Alias\":[\"" + str + "\"],\"Tags\":[],\"Title\":\"" + itembody.Title + "\",\"Content\":\"" + itembody.Content + "\",\"Sound\":\"\"}],\"Additional\":[]}]";
            _json = "[" + _json + "]";

            this.WriteAjaxMessage("PostMessage('" + _json + "','" + RowGuid + "');");//该处json变量前加'',是参数是json格式字符串,而不是json
        }

3、 Json数据模型

  public class MessagePush
    {
        public string Header { get; set; }
        public string Flag { get; set; }    
        public List Body { get; set; }
       public string[] Additional { get; set; }     
    }

    public class Body
    {
        public string Alert { get; set; }
        public string[] Alias { get; set; }     
        public string Title { get; set; }
        public string[] Extras { get; set; }   
    }

4、 后台ajax调用方法
//推送成功后,修改该通知推送状态

       [WebMethod]
        public static string PostSuccess(string RGuid)
        {
            //推送成功设置
            DataView dv = DB.ExecuteDataView("update InfoPushManager set Is_Push='1' where RowGuid='" + RGuid + "'");
            return "success";
        }

5、 前台脚本方法

    function PostMessage(json, rowguid) {
            $.ajax({
                //beforeSend: function (xhr) {
                //     xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
                //},
                url: "ip地址/api/Push/PushAliasAlert/v1?param=" + json,
                type: "GET",
                datatype: 'json',
                success: function (data) {
                    if (data[0].Flag == "error") {
                        alert("远程服务器返回错误,通知推送失败!");
                    }
                    else {
                        //使用ajax调用后台方法,更改该推送信息的是否推送状态
                        $.ajax({
                            type: "POST",
                            url: "MGInfoPush_List.aspx/PostSuccess",
                            data: "{'RGuid': '" + rowguid + "' }",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (data) {
                                if (data.d == "success") {
                                    location = location;//重新刷新界面
                                    layer.alert('通知推送成功!', { icon: 6 });
                                }
                                else {
                                    layer.alert('出现异常,通知推送失败!', { icon: 5 });
                                }
                            },
                            error: function (err) {
                                layer.alert('出现异常,通知推送失败', { icon: 5 });
                            }
                        });
                    }
                },
                error: function (data) {
                    layer.alert('出现异常,通知推送失败', { icon: 5 });
                }
            });
        }

你可能感兴趣的:(Jquery,JSON,AJAX,WebApi)