0

我正在尝试使用 setInterval 每 X 秒调用一次 LoadFile() 函数,并且我想在我的文档就绪函数中调用它,并且我的 aspx 页面上的脚本管理器将 EnablePageMethods 设置为 true。但这似乎不起作用并返回 PageMethod not defined 错误。我的代码如下


<script type="text/javascript" >
    $(document).ready(function () {
        setInterval(function () {
            PageMethods.LoadFile();              
        }, 1000); 
    });
</script>

    [WebMethod]
    public void LoadFile()
    {
        Collection<string> stringcollection = new Collection<string>();
        divLogSummary2.InnerHtml = "";
        try
        {
            StringBuilder content = new StringBuilder();
            if (string.IsNullOrEmpty(logFilePath))
            {
                return;
            }

            if (File.Exists(logFilePath))
            {

                using (FileStream fs = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        while (!sr.EndOfStream)
                        {
                             stringcollection.Add(sr.ReadLine());
                        }
                    }                        

                }
                for (int i = stringcollection.Count - 30 ; i < stringcollection.Count; i++)
                {
                    divLogSummary2.InnerHtml = divLogSummary2.InnerHtml + " <BR> " +                       stringcollection[i];
                }

            }
        }
        catch (Exception)
        {

        }
    }

我对 Ajax 比较陌生,非常感谢任何帮助和见解。

谢谢,

4

1 回答 1

0

您的方法应声明为静态,以便与 PageMethods 一起使用。

[WebMethod]
public static void LoadFile()
{
  .....
}

最好的祝福

于 2012-05-09T10:08:33.957 回答