2

I have a condition where i need to call a jquery function using a webmethod as below:

[WebMethod]
public static void BindData(String Site)
{
    String HTML = "";
    Int32 i = 1;
    DataTable dt = new DataTable();
    dt = obj.GetAll(objprop);
    if (dt.Rows[0]["UserId"].ToString() != "0")
    {
        foreach (DataRow item in dt.Rows)
        {
            string Email = Bal.Common.Decryptdata(item["Email"].ToString());
            string SentInvitation = item["SentInvitation"].ToString();

            SentInvitation = SentInvitation.ToString() == "1" ? "Already Invited" : "";
            if (i % 2 == 0)
                HTML += "<div class=~other_wish_row2~><div class=~friend_list_box1~><input type=~checkbox~ class=~chkEmail~ id=~chkId^" + i + "~/></div><div class=~friend_list_box2~><p><label id=~lbl" + i + "~>" + Email.ToString() + "</label><label class=~SentInvitationLbl~ id=~lblSentInvitation" + i + "~>" + SentInvitation + "</label></p></div><div class=~friend_list_box3~></div><div class=~clear~></div></div>";
            else
                HTML += "<div class=~other_wish_row3~><div class=~friend_list_box1~><input type=~checkbox~ class=~chkEmail~ id=~chkId^" + i + "~/></div><div class=~friend_list_box2~><p><label id=~lbl" + i + "~>" + Email.ToString() + "</label><label class=~SentInvitationLbl~ id=~lblSentInvitation" + i + "~>" + SentInvitation + "</label></p></div><div class=~friend_list_box3~></div><div class=~clear~></div></div>";

            i = i + 1;
        }
        ScriptManager.RegisterStartupScript((Page)(HttpContext.Current.Handler), typeof(Page), "hdrEmpty1", "Test('" + HTML + "');", true); return;
    }
    else
    {

    }
}

Jquery Code is:

function Test(data) {
   alert('hi');
}

function Binddata(SocialSite) {

$.ajax({
    type: "POST",
    url: "InviteFriends.aspx/BindData",
    data: "{SocialSite:'" + SocialSite + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {

    }
 });
}

I am not able to fire Test(), please help to resolve this.

4

1 回答 1

2

您不能从 Web 服务(.asmx 文件或 WebMethod)执行此操作,因为它不在普通页面的上下文中运行。我看到您正在使用 AJAX,您必须在调用的success回调方法中进行处理jQuery.ajax(),如下所示:

 function Test(data) {
    alert('hi');
 }

 function Binddata(SocialSite) {

 $.ajax({
     type: "POST",
     url: "InviteFriends.aspx/BindData",
     data: "{SocialSite:'" + SocialSite + "'}",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (data) {
         Test(data);
     }
  });
 }
于 2013-09-03T09:21:34.473 回答