I've created a windows service that runs locally and provides a web service which can be communicated with via ajax calls. I'm trying to write some code to detect if the web service is running on localhost so I can prompt users to install the application if it isn't running. When the service isn't running, the .ajax call isn't triggering the error callback, even after a timeout elapses. 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 running, both complete and success are called. When the service is stopped I never get the complete or error alert.
Any ideas why the callback is never getting triggered? Are there any other ways to see if the service is running then by simply calling one of the methods?
UPDATE: Fiddler doesn't report anything on 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 running, the subdomain works as expected, when the service is not running, 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 making the call though. The error callback is never triggered.
UPDATE 2: This is a complete working example you should be able to test with.
<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>
Unless you have a service running on localhost:12345 the call should fail, but never trigger error or complete.