Reputation: 10638
I have a jquery-ajax that is called several times with different IP each time. Then jquery-ajax calls an action in the mvc4 controller that is in charge of performing the ping and then return the results.
From Internet Explorer dev tools, I noted that all the ping requests are marked as pending and they are served one by one. Once one ping request is served, next one in the queue is served and so on. What happens is that last ping requests suddenly are canceled/aborted/rejected automatically. Each ping request consists on 4 retries and each retry with a timeout of 5 seconds. However, If I set a lower timeout for each retry, for example, 750 milli-seconds instead of 5 seconds then all work perfectly. So I would like to know why the last ping requests are being cancelled automatically... It seems that last ones are rejected because they take a "long" time before they can be served.
I can post here some code if needed, please, let me know.
I am using jquery-1.10.2
UPDATED:
ajax({
url: "/Tests/Ping/",
data: { IPAddress: IP },
type: 'POST',
dataType: 'json'
}).then(function (data) {
// Do some stuff on success
},
function (data) {
// Do some stuff on error
// here I am receiving for last ping requests:
// readyState: 0
// responseText: ""
// status: 0
// statusText: "error"
});
Action in the controller:
[HttpPost]
public ActionResult Ping(string IPAddress)
{
(...)
System.Net.NetworkInformation.Ping pinger = new System.Net.NetworkInformation.Ping();
(...)
string data = new String('a', 32);
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 5000;
for (int i = 0; i < 4; i++)
{
reply = pinger.Send(IPAddress, timeout, buffer);
(...)
}
(...)
Response.ContentType = "application/json;charset=utf-8";
Response.StatusCode = (int)(packetsLost < 4 ? HttpStatusCode.OK : HttpStatusCode.NotFound);
return new JsonResult()
{
Data = new
{
sent = 4,
received = 4- packetsLost,
lost = packetsLost,
percentLost = (int)(packetsLost / 4* 100)
}
}
}
See that (...) means more code. Also I need to return http status code plus json object in order to force jquery-ajax call to be entered in the success or error section in 'then'. This code is working ok for a lot of ping requests but as said, the last ping requests that do exactly the same as previous that went ok, are marked as cancelled/aborted/rejected.
Upvotes: 0
Views: 1802
Reputation: 4358
Instead of sending separate request for each IP address you could do the following -
thread.join
to make your webreference main thread wait for the ping threads to complete, else the ping threads will be terminated before they can complete their task.Upvotes: 1
Reputation: 2511
If the last requests have the same post data, there may be some browser caching issue going on. I would add a cache-buster parameter to be safe.
Your jquery ajax calls are likely running into a timeout scenario. From the jquery docs
timeout
Type: Number
Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent.
The reason they are timing out is because all browsers will automatically queue multiple requests to the same host so that there are a maximum of 2 concurrent requests at any given time. So if you request 3 files from www.example.com, the 3rd one won't start being requested until the first two are done.
You can get around this by:
Upvotes: 1