kc3ase
kc3ase

Reputation: 85

AJAX doesn't pick up error

I'm completely new to JSON, JS and AJAX. I'm used some example codes to progress through my targets. What I don't understand is when the JSON is an error object the success section is still firing. The console shows error where object d is null or a non-object when error is true. What have I done incorrectly? TIA

Result type good:

JSON: {"data":["1","breast","2","wing","3","thigh","4","leg","5","half","6","quarter white","7","quarter dark","9","whole"],"error":false}

Result type error:

JSON: {"error":true}

$.ajax({
            type     : 'GET',
            url      : 'getsubtypes2.php',
            data     : dataString,
            dataType : 'JSON',
            cache: false,
            success  : function(data) {          
                var output;            
                var d = data.data;
                    var output = "";
                    for (var i = 0 ; i< d.length; i=i+2) {//error gets to this line
                      var count = d[i];
                      var newOption = d[i+1];
                      output += "<option value='"+count+"'>"+newOption+"</option>";
                    }
                    $('#select3').empty().append(output); 
            },
            error: function(){
            $('#select3').empty();
                    console.log("Ajax failed");
            }
        });

Upvotes: 2

Views: 100

Answers (3)

kc3ase
kc3ase

Reputation: 85

As suggested this picks up the error correctly

$.ajax({
            type     : 'GET',
            url      : 'getsubtypes2.php',
            data     : dataString,
            dataType : 'JSON',
            cache: false,
            success  : function(data) {          
                var output;            
                var d = data.data;
                var e = data.error;
            if(e) {
                 $('#select3').empty();
            }
            else {
            var output = "";
                    for (var i = 0 ; i< d.length; i=i+2) {
                      var count = d[i];
                      var newOption = d[i+1];
                      output += "<option value='"+count+"'>"+newOption+"</option>";
                    }
                    $('#select3').empty().append(output); 
                }
            },
            error: function(){
            console.log("Ajax failed");
            }
        }); 

Upvotes: 0

PaulProgrammer
PaulProgrammer

Reputation: 17620

JQuery's $.ajax() routine relies on the HTTP response code to detect an error. If the service is returning a 200, even in an error condition (which does happen with some services) this could happen.

Try checking the network panel in your debugger to see what the service is really returning.

Upvotes: 2

Alexander Staroselsky
Alexander Staroselsky

Reputation: 38757

The server must return an error status code such as 4xx or 5xx for the error callback to execute. If you server is returning a 2xx, the success callback will execute.

For testing you can try explicitly sending an error code to see the error callback executing. See this question.

Upvotes: 2

Related Questions