天天看點

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[]["Info_Title"]);
            string ContentPush = Convert.ToString(Dv[]["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[];
            MP.Additional = tempAdditional;
            List<Body> body = new List<Body>();
            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 = ; i < Dv.Count; i++)
            {
                temp[i] = Convert.ToString(Dv[i][]);
            }
            itembody.Alias = temp;
            itembody.Title = Title;
            string[] Extras = new string[];
            itembody.Extras = Extras;
            string[] tempTags = new string[];
            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> 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[].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:  });
                                }
                                else {
                                    layer.alert('出現異常,通知推送失敗!', { icon:  });
                                }
                            },
                            error: function (err) {
                                layer.alert('出現異常,通知推送失敗', { icon:  });
                            }
                        });
                    }
                },
                error: function (data) {
                    layer.alert('出現異常,通知推送失敗', { icon:  });
                }
            });
        }