$.ajax 状态码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9097776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:07:19  来源:igfitidea点击:

$.ajax statusCode

ajax

提问by kakush

I have a problem in my code . This is part of it:

我的代码有问题。这是其中的一部分:

 function checkIfAvailable(username){
 var url = "/checkAvail?user="+username;
 var ans =  $.ajax({
      url: url,
      type: "GET",
      context: document.body,
      statusCode: {
        404: function() {
          console.log("-1-1-1-1 WE GOT 404!");
        },
        200: function() {
          console.log("-1-1-1-1 WE GOT 404!");
        }
      }
    });
         }

I think the response.status is 200, but I don't enter the '200' part. So how can I print the response.status I'm getting?

我认为 response.status 是 200,但我没有输入“200”部分。那么如何打印我得到的 response.status 呢?

回答by Josh Jones

success(data, textStatus, jqXHR){
    var statusCode = jqXHR.status;
    var statusText = jqXHR.statusText;
}

See jQuery API for more options...

有关更多选项,请参阅 jQuery API...

回答by mgraph

function checkIfAvailable(username) {
    var ans = $.ajax({
        url: "/checkAvail",
        type: "GET",
        data: "user=" + username,
        dataType: "html",//change it by the data type you get (XML...)
        context: document.body,
        statusCode: {
            404: function() {
                console.log("-1-1-1-1 WE GOT 404!");
            },
            200: function() {
                console.log("-1-1-1-1 WE GOT 200!");
            }
        },
        success: function() {
            console.log("success");
        },
        error: function(e) {
            alert(e);
        }
    });
}

回答by Muthu Kumaran

Message inside console.log("-1-1-1-1 WE GOT 404!") is same for both 404 and 200. change the message for 200 like console.log("Success.....")

console.log("-1-1-1-1 WE GOT 404!") 中的消息对于 404 和 200 都是相同的。将消息更改为 200,如 console.log("Success.....")