tags:

views:

888

answers:

2

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.

A: 

I assume that your webpage is being served from a different domain (or port).
Therefore, you will get a same-domain error if you try to request a different domain.

Since you said that it works fine when the service is running, I assume that the service has a CrossDomain.xml that allows the site to use it.

When the service isn't running, this file is inaccessible.

SLaks
We are using JQuery's jsonp to work around the cross domain scripting problem. Even with no response, shouldn't the jquery .ajax call hit a timeout or report an error of some sort? I need an error or some sort of triggered timeout to confirm that the service isn't running so I can display the appropriate actions for the user.
Timothy Strimple
A: 

I can work around the issue using the jquery timer plugin (http://plugins.jquery.com/project/timers) but I would still like to know why the .ajax call simply fails without any sort of notification when the service isn't running on localhost.

Below is the behavior I'd expect if the service isn't working.

<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" src="jquery_timers-1.2.js"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function(){
            $('#messages').append("Starting<br />");

            $('#messages').oneTime(1000, function(){
                $('#messages').append('Could not find service');
            });

            $.ajax({ url: 'http://localhost:12345/GetSomething/?callback=?',
            dataType: 'jsonp',
            timeout: 1000,
            complete: function() {
                $('#messages').append("Complete<br />");
                $('#messages').stopTime();
            },
            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 run this, the timer is never stopped because the complete action is never reached. So the timer elapses and reports that it cannot connect to the service.

Timothy Strimple