gpt4 book ai didi

ajax - 如何减少/消除 AJAX 请求中的等待时间/延迟

转载 作者:行者123 更新时间:2023-12-04 14:49:21 24 4
gpt4 key购买 nike

我有一个 ASP.NET MVC3 项目,其中主 aspx 页面正在使用 jQuery ajax 对其部件进行动态加载。所以基本上当网站加载时,它点击/Home/Index 然后在 Index (aspx) View 中,有几行 jQuery 进行 ajax 调用(到/Home/PartOne 和/Home/PartTwo 以填充页面的某些部分。

所以每次页面加载时,它基本上是在做 3 个请求:获取索引,获取 PartOne,然后是 PartTwo。

问题:为什么第三个请求执行需要某种“等待”时间?我以为这是浏览器并发请求限制,但为什么在第一个请求完成后不执行?

当我实验性地将“OutputCache”属性放在“PartTwo”上时,它的行为符合预期,执行速度很快。这暗示问题不在 IIS 中,而是在此之后和遇到我的操作方法之前的某个地方。

这是 Chrome 网络分析器的屏幕截图:
Chrome network profiler

这是 MvcMiniProfiler 上的屏幕截图 - 查看第 3 行/值,它在执行我的 Controller 的操作代码之前等待 500 毫秒。
enter image description here

我的 Controller 代码如下所示。虽然我截断了实际代码,但 PartTwo 的代码非常简单(没有长时间的计算,没有 db 调用等):

public class HomeController : Controller {
public ActionResult Index() {
// do something here
return View();
}

public ActionResult PartOne() {
// do something here
return View();
}

public ActionResult PartTwo() {
// do something here
return View();
}
}

我的javascript:
$(document).ready(function () {
$.ajax({
url: "/Home/PartOne",
cache: false,
success: function (data, textStatus, request) {
$("#TestContainerOne").html(data);
}
});

$.ajax({
url: "/Home/PartTwo",
cache: false,
success: function (data, textStatus, request) {
$("#TestContainerTwo").html(data);
}
});
});

我的 index.aspx:
<h2>Home page</h2>    
<div id="TestContainerOne"></div>
<div id="TestContainerTwo"></div>

PartOne.ascx:
<h2>Part One</h2>

PartTwo.ascx:
<h2>Part Two</h2>

帮助?

最佳答案

您应该使用只读 session 状态或完全禁用它,以便并行处理 Ajax 请求。默认情况下,服务器锁定来自同一客户端的请求的 session 状态,因此请求按顺序执行。

这是通过使用 SessionState 装饰您的 Controller 来完成的。属性:

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class HomeController : Controller {
public ActionResult Index() {
// do something here
return View();
}

public ActionResult PartOne() {
// do something here
return View();
}

public ActionResult PartTwo() {
// do something here
return View();
}
}

关于ajax - 如何减少/消除 AJAX 请求中的等待时间/延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9625268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com