jQuery function never reaches else

Asked by At

I've been attempting to check and see if a particular text file exists when a button is pressed, and display an alert regarding whether it does or not. The best way I've come up with to address this is to check and see if the file contains any text (as I hold the text in the event it is there for display later on anyway).

The issue I've been running into is that while it works perfectly if there is a guide, but it never alerts the user if the guide doesn't exist.

$(document).ready(function(){
    $('#check_for_build').click(function(){

        var results = 'https://mywebpage.com/build-867-5309.txt';

        jQuery.get(results, function(data){
            if($(data).val().length == 0){
                alert('No build is currently available.');
            }
            else{
                alert('There is a build for this.');
            }

        });
    });
});

I'm a little dumbfounded, since it's a pretty simple if/else statement! Thank you!

1 Answers

0
artptr On Best Solutions

If the guide does not exist, you get http 404 which isn't a 'success' code, so success handler isn't called. Try this variant:

jQuery.get(results, function (data) {
   alert('There is a build for this.');
}).fail(function (err) {
   alert('No build is currently available.');
});