处理$.ajax请求中的其他状态代码

Handles other status codes in $.ajax request

本文关键字:其他 状态 代码 ajax 请求 处理      更新时间:2023-09-26

在下面的代码中,我正在处理状态代码200和401。如果我想将控制权指向一个处理除200和401之外的所有代码的函数,该怎么办?

$.ajax({
    type: "POST",
    dataType: "json",
    data:POSTData,
    url: 'http://localhost/api/user/authenticate',
    statusCode: {
        200: function() {
            alert("ok");
        },
        401: function() {
            alert("Invalid Credentials");
        }
    }
});

试试这样的东西:

$.ajax({
    type: "POST",
    dataType: "json",
    data:POSTData,
    url: 'http://localhost/api/user/authenticate',
    complete: function(xhr, statusText){
        switch(xhr.status){
            case 200:
                alert("ok");
            case 401:
                alert("invalid credentials");
            ....
            etc
        }
    }
});