Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ReferenceError:未定义getMessage_C#_Jquery_Asp.net_Ajax_Web Services - Fatal编程技术网

C# ReferenceError:未定义getMessage

C# ReferenceError:未定义getMessage,c#,jquery,asp.net,ajax,web-services,C#,Jquery,Asp.net,Ajax,Web Services,我正在构建一个类似于facebook的消息传递区域,我正在使用ajax和jquery以及asmx web服务向客户端提供html。当内容第一次使用c#加载到页面加载时,我的li click事件起作用,但当ajax运行并刷新web服务中的内容时,li事件不再起作用 这是一个从web服务返回的html示例 <ol class="messagesrow" id="messages"> <li id="2345"> <div>Test Element</div&

我正在构建一个类似于facebook的消息传递区域,我正在使用ajax和jquery以及asmx web服务向客户端提供html。当内容第一次使用c#加载到页面加载时,我的li click事件起作用,但当ajax运行并刷新web服务中的内容时,li事件不再起作用

这是一个从web服务返回的html示例

<ol class="messagesrow" id="messages">
<li id="2345">
<div>Test Element</div>
</li>
</ol>

  • 测试元件
  • Web服务标记

    [WebMethod]
    public string GetMessagesByObject(string id, string objectid, string userid, string timezone)
    {
        string output = string.Empty;
    
        try
        {
            StringBuilder str = new StringBuilder();
    
            DataSet results = results from store procedure
    
            str.Append("<ol class=\"messagesrow\" id=\"messages\">");
            for (int i = 0; i < results.Tables[0].Rows.Count; i++)
            {
                DataRow row = results.Tables[0].Rows[i];
    
                DateTime date = Convert.ToDateTime(row["CreateDate"].ToString()).AddHours(Convert.ToDouble(timezone));
    
                if (!TDG.Common.CheckStringForEmpty(row["ParentMessageID"].ToString()))
                {
                    str.Append("<li id=\"" + row["ParentMessageID"].ToString() + "\">");
                }
                else
                {
                    str.Append("<li id=\"" + row["MessageID"].ToString() + "\">");
                }
    
                str.Append("<div style=\"width:100%; cursor:pointer;\">");
    
                str.Append("<div style=\"float:left; width:25%;\">");
                if (!TDG.Common.CheckStringForEmpty(row["ImageID"].ToString()))
                {
                    str.Append("<img src=\"/Resources/getThumbnailImage.ashx?w=48&h=48&id=" + row["ImageID"].ToString() + "\" alt=\"View Profile\" />");
                }
                else
                {
                    str.Append("<img src=\"/images/noProfileImage.gif\" alt=\"View Profile\" />");
                }
                str.Append("</div>");
    
                str.Append("<div style=\"float:left; width:75%; padding-top:4px;\">");
                str.Append("<strong>" + row["WholeName"].ToString() + "</strong>");
                str.Append("<br />");
                if (row["BodyMessage"].ToString().Length < 35)
                {
                    str.Append("<span class=\"smallText\">" + row["BodyMessage"].ToString() + "</span>");
                }
                else
                {
                    str.Append("<span class=\"smallText\">" + row["BodyMessage"].ToString().Substring(0, 35) + "</span>");
                }
                str.Append("<br /><span class=\"smallGreyText\">" + String.Format("{0:g}", date) + "</span>");
                str.Append("</div>");
    
                str.Append("</div>");
                str.Append("</li>");
            }
            str.Append("</ol>");
    
            output = str.ToString();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
        return output;
    }
    
    [WebMethod]
    公共字符串GetMessagesByObject(字符串id、字符串objectid、字符串用户id、字符串时区)
    {
    字符串输出=string.Empty;
    尝试
    {
    StringBuilder str=新的StringBuilder();
    数据集结果=存储过程的结果
    str.Append(“”);
    对于(int i=0;i”;
    }
    其他的
    {
    str.Append(“
  • ”; } str.Append(“”); str.Append(“”); if(!TDG.Common.CheckStringForEmpty(第[“ImageID”].ToString()行) { str.Append(“”); } 其他的 { str.Append(“”); } str.Append(“”); str.Append(“”); str.Append(“”+行[“WholeName”].ToString()+””; str.Append(“
    ”); 如果(第[“BodyMessage”].ToString()行长度<35) { str.Append(“+row[“BodyMessage”].ToString()+”); } 其他的 { str.Append(“+row[“BodyMessage”].ToString().Substring(0,35)+”); } str.Append(“
    ”+String.Format(“{0:g}”,date)+”); str.Append(“”); str.Append(“”); str.Append(“
  • ”); } str.Append(“”); 输出=str.ToString(); } 捕获(例外情况除外) { 掷骰子; } 返回输出; }
    Jquery标记

    $(document).ready(function () {        
    
        $("ol#messages li").click(function () {
            var id = $(this).attr("id");
    
            getMessage(id);
        });
    });
    
    function getMessage(id) {
    
            var timezone = $('#<%= hdfTimezone.ClientID %>').val()
            var userid = $('#<%= hdfUserID.ClientID %>').val()
    
            $.ajax({
                type: "POST",
                async: false,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                url: "/Resources/MessageWebService.asmx/GetMessage",
                data: "{'id':'" + id + "','timezone':'" + timezone + "','userid':'" + userid + "' }",
                success: function (data) { 
                    $('#<%= hdfMessageID.ClientID %>').val(id);
                    $('#<%= ltlMessages.ClientID %>').html(data.d);
                },
                error: function (data) {
                    showError(data.responseText);
                }
            });
    
        }
    
    $(文档).ready(函数(){
    $(“ol#messages li”)。单击(函数(){
    var id=$(this.attr(“id”);
    获取消息(id);
    });
    });
    函数getMessage(id){
    var timezone=$('#').val()
    var userid=$('#').val()
    $.ajax({
    类型:“POST”,
    async:false,
    数据类型:“json”,
    contentType:“应用程序/json;字符集=utf-8”,
    url:“/Resources/MessageWebService.asmx/GetMessage”,
    数据:“{'id':'”+id+“,'timezone':'“+timezone+”,'userid':'“+userid+”}”,
    成功:函数(数据){
    $('#').val(id);
    $('#').html(data.d);
    },
    错误:函数(数据){
    淋浴ROR(data.responseText);
    }
    });
    }
    
    由于列表项是动态的,因此您应该从ol委派事件

    $(document).ready(function () {        
    
        $("#messages").delegate("li","click",function () {
            getMessage(this.id);
        });
    
    });
    

    您得到的错误
    ReferenceError:getMessage not defined
    不应该发生在给定的代码中。

    您所说的“不工作”是什么意思?您是否尝试在浏览器中调试Javascript以查找故障点?您得到的错误没有意义,但是事件停止触发,因为新元素没有绑定事件。你应该在这里使用.on方法的事件委派。这似乎是正确的答案,为什么不将其作为答案发布呢?kevin,我尝试了.on方法,但它告诉我它不是函数。Im使用来自googleapis的jquery 1.6.4版library@chadn然后需要.delegate方法。