$.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");
}
}
答案 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");
}
}
它现在比较字符串,因为在大多数情况下,除非另有说明,否则响应将作为字符串返回