23

jquery Ajax呼び出しでは、現在、200と304のstatusCodeを処理しています。ただし、「エラー」を定義して、戻ってくる可能性のあるエラーをキャッチします。

関連する検証メッセージがある場合は、ステータスコード400-BadRequestを返します。

次に、これは「エラー」関数に分類されてから、定義したstatusCode「400」関数に分類されます。これは、2つのアクションが発生することを意味します。

理想的には、「Error」と「Success」を定義せず、「statusCode」のみを定義したいのですが、必要なのは「Else」を使用して、2〜3個のIのみが存在するすべてのstatusCodeを宣言する必要がないようにすることです。別の方法で処理したい。

$.ajax({
        type: 'POST',
        contentType: "application/json",
        url: "../API/Employees.svc/" + EmployeeId + "/Company/" + CompanyId,
        data: jsonString,
        statusCode: {
            200: function () { //Employee_Company saved now updated

                hideLoading();
                ShowAlertMessage(SaveSuccessful, 2000);
                $('#ManageEmployee').dialog('close');

            },
            304: function () { //Nothing to save to Employee_Company

                hideLoading();
                $('#ManageEmployee').dialog('close');

                if (NothingToChange_Employee) {
                    ShowAlertMessage(NothingToUpdate, 2000);
                } else {
                    ShowAlertMessage(SaveSuccessful, 2000);
                }
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            AjaxError(XMLHttpRequest, textStatus, errorThrown);
        }
    });
4

4 に答える 4

30

「complete」イベントは常に発生するため、そこからステータスコードを取得し、成功関数とエラー関数を無視することができます。

complete: function(e, xhr, settings){
    if(e.status === 200){

    }else if(e.status === 304){

    }else{

    }
}
于 2012-04-06T10:16:54.367 に答える
10

これは私が使用するものです:

error: function (xhr, textStatus, errorThrown) {
    switch (xhr.status) {
        case 401:
           // handle unauthorized
           break;
        default:
           AjaxError(xhr, textStatus, errorThrown);
           break;
    }
}
于 2012-12-16T19:55:14.667 に答える
1

最初のロジックと同様のアプローチを維持するために、statusCode オブジェクトを引き続き渡します。ただし、「else」が 4xx または 5xx タイプのエラー コードの領域に分類されることはまだわかっています。

したがって、元のコードを次のように更新します。

var statusCodeResponses = {
    200: function () { //Employee_Company saved now updated

        hideLoading();
        ShowAlertMessage(SaveSuccessful, 2000);
        $('#ManageEmployee').dialog('close');

    },
    304: function () { //Nothing to save to Employee_Company

        hideLoading();
        $('#ManageEmployee').dialog('close');

        if (NothingToChange_Employee) {
            ShowAlertMessage(NothingToUpdate, 2000);
        } else {
           ShowAlertMessage(SaveSuccessful, 2000);
        }
    }
};

var genericElseFunction = function(response){
    // do whatever other action you wanted to take
};

for(var badResponseCode=400; badResponseCode<=599; badResponseCode++){
     statusCodeResponses[badResponseCode] = genericElseFunction;
}

$.ajax({
    type: 'POST',
    contentType: "application/json",
    url: "../API/Employees.svc/" + EmployeeId + "/Company/" + CompanyId,
    data: jsonString,
    statusCode: statusCodeResponses,
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        AjaxError(XMLHttpRequest, textStatus, errorThrown);
    }
});
于 2015-01-07T18:25:14.643 に答える