$.ajax statusCode

后端 未结 3 1304
萌比男神i
萌比男神i 2021-02-11 05:20

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

 function checkIfAvailable(username){
 var url = \"/checkAvail?user=\"+username;
 var ans =  $.ajax({
      url         


        
相关标签:
3条回答
  • 2021-02-11 05:30
    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);
            }
        });
    }
    
    0 讨论(0)
  • 2021-02-11 05:36

    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.....")

    0 讨论(0)
  • 2021-02-11 05:40
    success(data, textStatus, jqXHR){
        var statusCode = jqXHR.status;
        var statusText = jqXHR.statusText;
    }
    

    See jQuery API for more options...

    0 讨论(0)
提交回复
热议问题