Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.ajax statusCode

Tags:

ajax

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?

like image 257
kakush Avatar asked May 12 '26 19:05

kakush


2 Answers

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

See jQuery API for more options...

like image 123
Josh Jones Avatar answered May 14 '26 22:05

Josh Jones


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);
        }
    });
}
like image 23
mgraph Avatar answered May 14 '26 21:05

mgraph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!