3

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.

5
  • Run Fiddler and see what happens. Commented Feb 21, 2010 at 4:01
  • Fiddler doesn't show any requests when I hit localhost or 127.0.0.1, even when the service is responding. Commented Feb 21, 2010 at 4:22
  • What browser do you use to test this, I tested your code in FF and it worked fine. In IE it doesn't seem to work. IE says: Access is Denied. Commented Feb 21, 2010 at 5:00
  • The file is displayed with a file:/// - style URI? Commented Feb 21, 2010 at 5:04
  • I've tested in the latest versions of IE, FF and Chrome. No luck getting an error response from any of them. I've added a complete working example and added it to the original question. Commented Feb 21, 2010 at 7:53

3 Answers 3

1

This has been fixed in jquery 1.5. The script in the original post works as expected now and I can get rid of the timer workaround.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.