问题
I'm not sure where the problem is...
I have an ajax request that checks the tracking information for a package on a page with a list of packages:
$(".fedex_status").each(function() {
var item = this;
// some code to construct tracking_url
$.ajax({
type: "GET",
url: tracking_url,
async: true,
cache: false,
success: function(data) { $(item).html("(" + data + ")"); },
error: function() { $(item).html("request failed...");}
});
});
So if there are 10 packages on the page (10 things with class 'fedex_status') 10 requests are created. The requests work fine, but results are returned one at a time (in a serial manner). I added a timestamp to the start and stop of a request within the controller action:
public ActionResult Fedex(string trackingNumber)
{
DateTime requestStart = DateTime.Now;
TrackingService tracking = new TrackingService();
string status = tracking.FedexTrackingNumberStatus(trackingNumber);
return Content(status + " - " + requestStart.ToString("hh:mm:ss.FFF") + " - " + DateTime.Now.ToString("hh:mm:ss.FFF"));
}
None of the timestamps overlap. So the controller is processing the requests one at a time. This seems crappy.
Now, the ajax request 'should' be parallel. It definitely returns immediately (its asynchronous). When I look at the IIS logs the requests have the same timestamp as the controller action return.
So my question is: is jquery not sending all the ajax requests in parallel or is IIS or ASP.NET only processing requests serially. I'm a big noob with IIS and the technical details of ASP.NET, so maybe its been misconfigured forever and only responds to one request at a time on everything (its internal use only, low traffic). That said, I'm also an idiot at jquery and not sure how to test when the requests are actually being fired (all I know is the $.ajax call returns immediately).
Thanks!
回答1:
In theory, ASP.NET with Session state enabled locks the session for each user request, thus processing them serially. I don't think you can disable Session for an MVC project painlessly, because TempData for example uses session by default. But you can try.
EDIT: here's a related question
回答2:
Your code works as expected on my machine. I had to insert any artificial delay with Thread.Sleep to see it in action though because the unadulterated requests executed so fast that it appeared they were all completed at once.
System.Threading.Thread.Sleep(1000);
return Content(String.Format("Thread #{0}, Started = {1:hh:mm:ss.FFF}, Completed = {2:hh:mm:ss.FFF}, Duration = {3:hh:mm:ss.FFF}, Result = {4}",
System.Threading.Thread.CurrentThread.ManagedThreadId,
requestStart,
DateTime.Now,
DateTime.Now.Subtract(requestStart),
status));
A quick test on my machine using Firefox and connecting to the built-in Visual Studio Web Server showed that two requests were executed simultaneously on the client and handled by two different threads on the server.
As far as I know, there are a limited number of allowed concurrent requests allowed by the browser. Here is another post that on SO that covers what each value is for each of the popular browsers: How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?
EDIT: I reread your post and saw that you mentioned IIS 6. I do not have access to IIS 6, but running it on an IIS 7 Web Server with a 5 second artificial timeout (Thread.Sleep) showed that multiple requests were being handled at the same time by different server threads. None of them start at the EXACT same time, so there might be some queuing going on at the server, but with a 5 second delay it's easy to see that in Firefox 3.0 I have at least 6 concurrent requests at a time.
回答3:
You are calling your ajax request every time you find an element with the class .fedex_status.
Your line
$(".fedex_status").each
is saying do this everytime you find an element with the class .fedex_status.
来源:https://stackoverflow.com/questions/860478/asp-net-mvc-jquery-iis6-multiple-ajax-requests