views:

58

answers:

3

I do a bunch of $.post()'s in my script, within a $(document).ready(function(){

jQuery says:

If a request with jQuery.post() returns an error code, it will fail silently unless the script has also called the global .ajaxError() method.

I tried adding this as a first test

$(document).ajaxError(function(e, xhr, settings, exception) {
alert('error in: ' + settings.url + ' \\n'+'error:\\n' + exception);
});

But it failed to do anything. Can anyone assist? I just need to popup an alert if the internet connection is lost. I also tried using window.navigator.onLine for this but it's not supported in Safari

A: 

have you tried not binding it to document?

$('.log').ajaxError(function(e, xhr, settings, exception) {
   alert('error in: ' + settings.url + ' \\n'+'error:\\n' + exception);
});
Reigel
It should work the same either way, the event is fired for all elements bound to it...jQuery crawls the `$.cache` object to check which ones to fire it on.
Nick Craver
ahh Okay... I wonder what's the problem then...
Reigel
A: 

woops, looks like i managed to loose my association with the original post - i made this post.

Anyway to clarify, the code I have is:

  $(document).ready(function(){ 

$(this).ajaxError(function(e, xhr, settings, exception) {
    alert('error in: ' + settings.url + ' \\n'+'error:\\n' + exception);
});

$.post("dostuf.php", { action: "thething"  });

No error is being thrown.

mat
Your username and userid appear to have changed. Weird.
Keyo
@Keyo: that's his evil twin.
BoltClock
@Keyo, @BoltClock - He's logged in with *another* OpenID
Nick Craver
A: 

Not sure on .post(); but in the api for .ajax() there is an error function you can pass to the object.

$.ajax({
    type:"post",
    url:"http://yourwebservice.com/get_data",
    error:function(XMLHttpRequest, textStatus, errorThrown) { 
        //do stuff 
        alert('fail whale');
    },
    success:function(data) {
        alert('it worked!');
    }        
});

See http://api.jquery.com/jQuery.ajax/

By the way, welcome to stackoverflow!

Keyo