0

I am developing a webpage that is using cache.manifest to cache relevant files for offline use. However, I need to be able to detect if there is an actual internet connection. if(!navigator.onLine) only works up to a certain degree .. it tends to say it's online when it actually isn't. Therefor I am trying to create an ajax call that returns true if the call is successful, else return fail. I am no master at jQuery, but this is what I have so far. I have tried number of alternatives, but I always end up with an "undefined" return.

function checkIfOnline() {
var returned = true;
$.get('services/getObjects.ashx',
        {
            "get": "heartbeat",
            "hash": userHash,
            "uid": uid,
            "gid": gid
        },
        function (data) {
            if(data.result === "Success") {
                alert("Success");
            }
            console.log(data.result);
        },
        "json"
    )
.done(function () {alert("done");
                   returned = true;
                   return returned;})
.fail(function () {alert("fail");
                       returned = false;
                       return returned;});

}

Anyone out there that can see what I am doing wrong? alerts all show what they are supposed to ... I get "Success" alert, and then "done" or "fail" depending on the connection.

Update

Based on the thread that was pointed out, I made the following changes to my code

function checkIfOnline() {
    return $.ajax({
    type: 'GET',
        url: 'services/getObjects.ashx',
        data:
        {
            "get": "heartbeat",
            "hash": userHash,
            "uid": uid,
            "gid": gid
        }
    });
}

function getOnlineStatus() {
checkIfOnline().done(function(result) {
    alert("done - "+result.result);
    return true;
}).fail(function() {
    alert("failed");
    return false;
});
}

But I still get this annoying "undefined",but neither true or false (when calling getOnlineStatus() )

Sjonnino
  • 27
  • 7

2 Answers2

0

you are returning it wrong. instead of returning it from the done and fail function, return the value from outside the get call.

function checkIfOnline() {
var returned = true;
$.get('services/getObjects.ashx',
        {
            "get": "heartbeat",
            "hash": userHash,
            "uid": uid,
            "gid": gid
        },
        function (data) {
            if(data.result === "Success") {
                alert("Success");
            }
            console.log(data.result);
        },
        "json"
    )
.done(function () {alert("done");
    returned = true;
})
.fail(function () {alert("fail");
    returned = false;
});

return returned;
}

UPDATE:

The above answer is not completely correct. The correct implementation would be the use of deferred objects as mentioned in this answer

function foo() {
    return $.ajax(...);
}

foo().done(function(result) {
    // code depending on result
}).fail(function() {
    // an error occurred
});
Community
  • 1
  • 1
Mandeep Jain
  • 2,304
  • 4
  • 22
  • 38
  • After further testing I still have problems with returning the correct statement. Because of async this function always returns the initial value, but .done and .fail are fired up after the return statement. So basically this always returns true – Sjonnino Jul 26 '13 at 08:21
  • hmm. i see the issue there. my bad, the answer above is not completely correct. perhaps you should visit the above mentioned link http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Mandeep Jain Jul 26 '13 at 08:29
0

For those who stumle on to this. I was unable to get this to work using the deferred methods.

I simply went back to basisc and got it to work exactly how I wanted. Here is the code I used.

function checkIfOnline() {
var response = false;
$.ajax({
    type: 'GET',
    url: 'services/getObjects.ashx',
    async: false,
    data:
        {
            "get": "heartbeat",
            "hash": userHash,
            "uid": uid,
            "gid": gid
        },
    success: function(data) {
        if(data.result === "Success") {
            response = true;
            }
        }
    });
return response;
}
Sjonnino
  • 27
  • 7