ajax响应是整数,我无法处理它,当我提醒它我得到[对象,对象]

时间:2014-02-09 10:12:48

标签: jquery ajax jquery-ui

$.ajax({
        type: "POST",
        url: url_delete_admin_privilidge,
        data: postData,
        success: function(response) {
            console.log(response);
            setAdminResponse(response);
            alert("success");

        },
        fail: function() {
            alert("fail");
            console.log("fail");

        }
    });
  • 此请求的响应为1表示成功,0或2或3表示另一些情况如果不成功则总是在失败方法中输入并且请求状态为400(错误请求)我无法处理这些情况。

  • 我在setAdminResponse();处理了回复和请求案例,其中的代码总是在其他情况下输入。

    function setAdminResponse(response) {
    if (response === 1)
    {
        $("#Response").text("success").css("color", "green");
    }
    else if (response === 0)
    {
        $("#Response").text("user not found in circle").css("color", "red");
    } else if (response === 2) {
        $("#Response").text("user is already admin in circle").css("color", "red");
    
    } else if (response === 3)
    {
        $("#Response").text("only owner can do it").css("color", "red");
    } else
    {
        $("#Response").text("an error happenes").css("color", "red");
    }
    
    }
    

1 个答案:

答案 0 :(得分:0)

像这样修改你的代码:

$.ajax({
        type: "POST",
        url: url_delete_admin_privilidge,
        data: postData,
        success: function(response) {
            console.log(response);
            setAdminResponse(response);
            alert("success");

        },
        error: function() {
            alert("fail");
            console.log("fail");

        }
    });

功能:

function setAdminResponse(response) {
if (response === '1')
{
    $("#Response").text("success").css("color", "green");
}
else if (response === '0')
{
    $("#Response").text("user not found in circle").css("color", "red");
} else if (response === '2') {
    $("#Response").text("user is already admin in circle").css("color", "red");

} else if (response === '3')
{
    $("#Response").text("only owner can do it").css("color", "red");
} else
{
    $("#Response").text("an error happenes").css("color", "red");
}

}

它现在比较字符串,因为在大多数情况下,除非另有说明,否则响应将作为字符串返回