简体   繁体   English

如何处理ajax响应中的内部服务器错误

[英]How to handle internal server error in ajax response

When I send an Ajax request in JavaScript (with JQuery), it sometimes throws an internal server error that I can't catch. 当我用JavaScript(使用JQuery)发送Ajax请求时,它有时会抛出我无法捕获的内部服务器错误。 The error shows up in the browser. 该错误显示在浏览器中。 I even tried to wrap the ajax call in a try - catch block. 我甚至试图在一个try-catch块中包装ajax调用。 How can I handle the Ajax error? 我该如何处理Ajax错误?

EDIT here is my code: 编辑这里是我的代码:

$.post('/multi/getGameStatus', function(data) {
    if(data && data.game) {
        settings.game = data.game;
        setStartRacePopupUI.call(this, data);
        // remove all racers
        removePlayers.call();
        for(var i=0;i<settings.game.players.length;i++) {
            var player = settings.game.players[i];
            var isme = (player.id == settings.playerId);
            addPlayer.call(this, player, isme, i);
        }

        if (settings.game.gameStatus == "OPEN") {
            setTimeout(refreshPlayers, refreshPlayersInterval, nextStatus);
        } else if(settings.game.gameStatus == "IN_GAME") {
            counterToGameStart = data.sts;
            gameFllow(nextStatus);
        }
    }
});

Even if I use the error handler I still get a JS error on the page 即使我使用错误处理程序,我仍然在页面上得到JS错误

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

...For convenience and consistency with the callback names used by $.ajax(), jqXHR also provides .error(), .success(), and .complete() methods. ...为了方便和与$ .ajax()使用的回调名称保持一致,jqXHR还提供.error(),. success()和.complete()方法。

The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. jq 1.8中将不推荐使用jqXHR.success(),jqXHR.error()和jqXHR.complete()回调。 To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead. 要准备最终删除的代码,请使用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax( "example.php" )
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
$.ajax({
  url:"myurl",
  data: {json:"data"},
  success: function(){//on success},
  error: function(){//called on error}
});

error callback as per ajax api docs : (when in doubt refer to api docs) 根据ajax api文档的错误回调:(当有疑问时请参考api文档)

error(jqXHR, textStatus, errorThrown)Function 错误(jqXHR,textStatus,errorThrown)函数

A function to be called if the request fails. 请求失败时要调用的函数。 The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. 该函数接收三个参数:jqXHR(在jQuery 1.4.x,XMLHttpRequest中)对象,描述发生的错误类型的字符串和可选的异常对象(如果发生)。 Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". 第二个参数的可能值(除了null)是“timeout”,“error”,“abort”和“parsererror”。 When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." 发生HTTP错误时,errorThrown会收到HTTP状态的文本部分,例如“Not Found”或“Internal Server Error”。 As of jQuery 1.5, the error setting can accept an array of functions. 从jQuery 1.5开始,错误设置可以接受一系列函数。 Each function will be called in turn. 每个函数将依次调用。 Note: This handler is not called for cross-domain script and JSONP requests. 注意:不会为跨域脚本和JSONP请求调用此处理程序。 This is an Ajax Event. 这是一个Ajax事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM