$ .ajax statusCode

kak*_*ush 2 ajax

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

 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!");
        }
      }
    });
         }
Run Code Online (Sandbox Code Playgroud)

我认为response.status是200,但我没有进入'200'部分.那么如何打印我所得到的response.status?

小智 9

success(data, textStatus, jqXHR){
    var statusCode = jqXHR.status;
    var statusText = jqXHR.statusText;
}
Run Code Online (Sandbox Code Playgroud)

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


mgr*_*aph 6

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);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)