0

What i'm trying to get this to do is to display different error messages, response code 200, 400, 404, 408 & 500 however, i don't think i'm do it right.

i looked at this one: jQuery AJAX Error Handling (HTTP Status Codes) but was still confused..

What i have below is probably messy, but any guidance?

            var request;  //make request object a global variable
            function getAJAX(){
              request = new XMLHttpRequest();
              request.open("GET", "file.txt");
              request.onreadystatechange = checkData;
              request.send(null);
            } // end function\

            function getYo(){
              request = new XMLHttpRequest();
              request.open("GET", "file2.txt");
              request.onreadystatechange = checkDataa;
              request.send(null);
            } // end function

            function checkData(){
              if (request.readyState == 4) {
                // if state is finished
                if (request.status == 200) {
                  // and if attempt was successful
                  alert(request.responseText);
                } // end if
              } // end if
            } // end checkData

            function checkDataa(){
              if (request.readyState == 4) {
                // if state is finished
                if (request.status == 400) {
                  // and if attempt was successful
                  alert(request.responseText);
                } // end if
              } // end if
            } // end checkData
Community
  • 1
  • 1

2 Answers2

0

You can create a simple function to use within both and pass in the status ( and anything else you may want):

function errorAlert( status ){
   var msg;
   switch(status}{
      case 404:
         msg  = "Ooooops can't find it!";
       break;
       default:
          msg = 'unknown error';
   }
   alert( msg );
}

Then in your request callback

if (request.readyState == 4) {
   if( request.status !== 200){
         errorAlert(  request.status );
         /* quit here */
         return;
   }
    /* handle response here */
}

This is obviously fairly primitive but should give you an idea how to build on it

charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

Utilizing jquery $.ajax() statusCode settings object , callback function , try

Edit, updated

html , e.g.,

<button id="error">error</button>
<button id="success">success</button>

js

$(function() {
    var urls = ["file.txt", "file1.txt"];
    var request = function (url) {
        return $.ajax({
        url : url, 
        type : "GET",
        statusCode : {
            200 : function (data, textStatus, jqxhr) {
                    alert(data);
            },
            403 : function (jqxhr, textStatus, errorThrown) {
                    alert(textStatus + "\n" + errorThrown);
            },
            404 : function (jqxhr, textStatus, errorThrown) {
                    alert(textStatus + "\n" + errorThrown);
            }
        }
    });
    };

$("button#error").on("click", function(e) {
    request(urls[0]);
});
$("button#success").on("click", function(e) {
    request(urls[1]);
});

});

jsfiddle http://jsfiddle.net/guest271314/XSQ3w/

guest271314
  • 1
  • 15
  • 104
  • 177