开发者

jQuery $.post execute success instead of error if connection to server is lost

开发者 https://www.devze.com 2023-03-30 06:58 出处:网络
I\'m using this code to connect to the server via jQuery var jqxhr = $.post(\'@Url.Action(\"../CheckMessage\")\',{ \'MessageId\': MessageId },

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(XMLHttpRequ开发者_C百科est, 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?


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");
     }
});

.

0

精彩评论

暂无评论...
验证码 换一张
取 消