【问题标题】:JQuery Retrieving Data from aspx pageJQuery 从 aspx 页面检索数据
【发布时间】:2012-07-04 16:54:53
【问题描述】:

我想从 aspx 页面检索数据。但是,如果填充,则等待静态字段中的数据。

jQuery :

   $.ajax({
        url: "Services/JSON/GetMessage.aspx",
        type: "GET",
        dataType: "json",
        success: function (response) { alert(response.Text); },
        error: function (x, t, m) {
            if (t === "timeout") {
                alert("got timeout");
            } else {
                alert(t);
            }
        }
    })

服务/JSON/GetMessage.aspx

<%@ Page Language="C#" %>
<% 
Response.Clear();
Response.ContentType = "text/json";
int tryCount = 0;
while (1 == 1)
{
    if (ClsStaticFields.Messages.Count > 0)
    {
        foreach(string message in ClsStaticFields.Messages) {
             Response.Write("{ Text:'" + message + "' }");
        }
    }
    tryCount ++;
    if (tryCount > 29) break; // 1 Minute wait and exit
    System.Threading.Thread.Sleep(2000); // 2 Second wait
}
Response.End();
%>

我很好奇:

  • 这个等待期会影响其他用户吗?
  • 这会导致 IIS 或页面上的锁定吗?
  • 这种用法是否对 CPU 造成严重影响?

【问题讨论】:

    标签: c# javascript jquery asp.net


    【解决方案1】:

    我看到您使用GetMessage.aspx 页面,aspx 页面可能与会话相关联,这会影响其他用户。

    如果可能并且一切正常,请禁用该页面的会话。

    <%@ Page Language="C#" EnableSessionState="false" %>
    

    亲戚:

    Web app blocked while processing another web app on sharing same session
    What perfmon counters are useful for identifying ASP.NET bottlenecks?
    Replacing ASP.Net's session entirely

    全景

    我认为你在这个闭环中等待消息的方式不是一个好主意,我建议有两种选择。

    在浏览器using a javascript timer 上定期调用。您每 2 秒获取一次消息的以太网方式,那么为什么不每隔几秒从浏览器调用该消息呢?

    或者更好的是使用comet方法调用服务器。这是 asp.net 中的彗星示例:http://www.aaronlerch.com/blog/2007/07/08/creating-comet-applications-with-aspnet/

    为什么不好,首先你使用属于asp.net iis的线程,并与浏览器调用连接,即留下一个打开的连接,很快你的打开连接就会用完,也许还有asp。净净线程有问题。

    像你一样定期检查最好使用定时器来完成,而不是用线程等待关闭循环,这里最好的地方是他自己的浏览器。

    【讨论】:

    • @AdemEyüboğlu 那么这段代码可能是锁定其余调用。
    猜你喜欢
    • 1970-01-01
    • 2019-12-24
    • 2018-06-04
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2015-10-24
    相关资源
    最近更新 更多