0

I'm using this code to connect to the server via jQuery

var jqxhr = $.post('@Url.Action("../CheckMessage")',  { 'MessageId': MessageId }, 
    function (data, s) {
        alert ('This is executed on success only - data: ' + data + 's: ' + s);
    }
)
.error(function(XMLHttpRequest, textStatus, errorThrown) { alert("error"})

If I create a new exception in the server .error method is correctly executed, but if I take the server down, then success method is executed. How can I check if the connection to the server was lost?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
polonskyg
  • 4,269
  • 9
  • 41
  • 93

1 Answers1

0

Ajax recall the cached data for subsequent request if you do not sepecify cache as false in the ajax setting. Try this cache:false using $.ajax.

$.ajax({
     url:  '@Url.Action("../CheckMessage")',
     data: { 'MessageId': MessageId }, 
     cache: false,
     success: function (data, s) {
        alert ('This is executed on success only - data: ' + data + 's: ' + s);
     },
     error: function(XMLHttpRequest, textStatus, errorThrown) { 
       alert("error");
     }
});

.

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • Answer from another thread: [http://stackoverflow.com/questions/5542701/setting-the-cache-in-jquery-post-to-be-false](http://stackoverflow.com/questions/5542701/setting-the-cache-in-jquery-post-to-be-false) $.post is not cached. Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests. From [http://api.jquery.com/jQuery.post/](http://api.jquery.com/jQuery.post/) So, it still doesn't recognize a server lost. Any ideas? – polonskyg Aug 27 '11 at 17:20