weixin_33724059 2012-10-05 16:02 采纳率: 0%
浏览 11

慢速ajax,可轻松访问数据库

While testing out setInterval(), i had a slow load time for an ajax request. It was very simple and other pages with much more complex requests were terminating in less than a second while this very simple one is terminating after 10-12. Without using setInterval() it is still taking this long and it uses the same layout as the other pages.

Example of script:

    document.getElementById("test").innerHTML = "Starting";
    if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }

    else
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            document.getElementById("test").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "/TestLive.cshtml", true);
    xmlhttp.send();

Slow server page (ASP.NET):

Response.Expires = -1;

var db = Database.Open("mydb");
string query = "select Name from Game where ID='97';";

Response.Write(db.QuerySingle(query).Name);

Example of fast:

    Response.Expires = -1;

string query = "select * from Season where Season.Game = 'Football' and Season.End > '" + DateTime.Now.Add(new TimeSpan(-8,0,0)).ToString("yyyy-MM-dd") + "';";

var db = Database.Open("mydb");
var result = db.Query(query);

Response.Write("<select id=\"chosenSeason\" onchange=\"setDraftSeason()\">");
Response.Write("<option selected=\"selected\">Choose a Season</option>");
foreach (var season in result)
{
    Response.Write("<option value=\"" + season.ID + "\">");
    Response.Write(season.Name);
    Response.Write(" (" + season.Start.ToString("dd-MM-yyyy") + " - " + season.End.ToString("dd-MM-yyyy") + ")");
    Response.Write("</option\">");
}
Response.Write("</select>");

EDIT: It seems to be related to using the layout page to access the script. all of the fast pages use the same setup though.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
    • ¥88 实在没有想法,需要个思路