How to reduce / eliminate latency / delay in an AJAX request

I have an ASP.NET MVC3 project where an aspx main page dynamically loads its parts using jQuery ajax. Thus, basically, when a site loads, it gets into / Home / Index, and then in the Index view (aspx) there are several jQuery lines that make ajax calls (in / Home / PartOne and / Home / PartTwo to fill in the parts pages.

Thus, every time a page loads, it basically makes 3 requests: get the index, get PartOne, and then PartTwo.

Question: why is there some kind of β€œwait” for the third request? I thought this was a limitation on the simultaneous request of the browser, but why is it not executed after the first request is made?

When I experimentally put the "OutputCache" attribute in "PartTwo", it behaves as expected to be fast. This indicates that the problem is not in IIS, but somewhere after that and before it hits my action method.

Here is a screenshot from the Chrome Network Profiler: Chrome network profiler

Here is a screenshot on MvcMiniProfiler - look at the third line / value, it waits 500 ms before executing the controller action code. enter image description here

My controller code looks like this. Although I cut off the actual code, but the code for PartTwo is very trivial (no long calculations, no db calls, etc.):

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(); } } 

My 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); } }); }); 

My 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> 

reference

+6
source share
1 answer

You should only use read-only session state or completely disable it to get parallel processing of Ajax requests. By default, the server blocks the session state for requests coming from the same client, so the requests are executed sequentially.

This is done by decorating your controller with the SessionState attribute:

 [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(); } } 
+7
source

Source: https://habr.com/ru/post/910283/


All Articles