I am having a problem checking if a file exists using $.ajax(). I am looping through a json file with $.each and I want to see if the specified staff member has an image, and if not use the no_photo.jpg image. I keep getting the no_photo.jpg image even though there is the staff member's image in the directory.
Here is the entire $.getJSON() call.
$.getJSON('staff.json', function(data) {
var last_check;
var image_url = "no_photo";
$.each(data.employees, function(i,employee){
if ((i+1)%4==0) {last_check = " last";} else {last_check = "";}
$.ajax({
url:'/images/staff/' + employee.first_name.toLowerCase() + employee.last_name.toLowerCase() + '.jpg',
type:'HEAD',
error:
function(){
image_url = "no_photo";
},
success:
function(){
image_url = employee.first_name.toLowerCase() + employee.last_name.toLowerCase();
}
});
$("div.staff-members").append('<a href="#" title="' + employee.first_name + ' ' + employee.last_name + ', ' + employee.title + '" rel="' + employee.location + '" class="' + employee.category + last_check + '"><img src="/images/staff/' + image_url + '.jpg"><span class="' + employee.category + '"></span></a>');
});
});