I created a Windows service that runs locally and provides a web service that can be exchanged using ajax calls. I am trying to write code to determine if a web service is running on localhost, so I can offer users to install the application if it is not running. When the service is down, the .ajax call does not cause an error callback even after the timeout expires. Here is the code I'm running right now
$.ajax({ url: 'http://localhost:12345/GetVersion/',
dataType: 'json',
timeout: 1000,
complete: function() {
alert('complete');
},
success: function() {
alert('success');
},
error: function() {
alert('error');
}
});
When the service is started, both full and successful are called. When the service is stopped, I never get a full message or error warning.
Any ideas why the callback never works? Are there other ways to see if a service is running just by calling one of the methods?
UPDATE: Fiddler reports nothing about localhost or 127.0.0.1 requests, so I created a subdomain for our domain that points to 127.0.0.1. When the service is started, the subdomain works as expected, when the service is down, fiddler gives me this error:[Fiddler] Connection to localhost.stayhealthy.com failed.Exception
Text: No connection could be made because the target machine actively refused it 127.0.0.1:49994
I still get the same results on the website that makes the call. The error callback never starts.
UPDATE 2: This is a complete working example with which you can check.
<html>
<head>
<title>Ajax Localhost Test</title>
<script language="javascript" type="text/javascript" src="jquery-1.4.2.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$('#messages').append("Starting<br />");
$.ajax({ url: 'http://localhost:12345/GetSomething/?callback=?',
dataType: 'json',
timeout: 1000,
complete: function() {
$('#messages').append("Complete<br />");
},
success: function(version) {
$('#messages').append("Success<br />");
},
error: function(request, status, error) {
$('#messages').append("Error<br />");
}
});
});
</script>
</head>
<body>
<h1>Ajax Localhost Test</h1>
<div id="messages"></div>
</body>
</html>
If you do not have a service running on localhost: 12345, the call should fail, but never launch an error or terminate.