如何忽略Ajax 404错误?

我的脚本尝试使用ajax加载图像URL。如果找不到图像,ajax会引发404错误,然后,脚本再加载后备图像。但是,我不喜欢浏览器控制台显示未找到的图像的404错误——这是消除错误的一种方法吗? 还是哪里出错了?

$.ajax({
    type: "post",
    url: try_img_url,
    success: function(response){

    //if finds the image, use it.
    },
    error: function(response){

          //if does not find the image, use fall back image.

    }
});

我的另一个想法是在进行ajax调用之前检查图像url是否存在,这样,如果图像不存在,我根本就不必进行调用。但是,我不确定该怎么做......

I'm not sure if this is possible with JS but I would approach this by adding a small PHP script like eg getimage.php then use that to check if the file exists.

PHP:

<?php
if (isset($_GET['image']) && is_file($_GET['image'])) {
    echo sprintf("<img src='%s' />", $_GET['image']);
} else {
    echo "<img src='fallback.jpg' />";
}
?>

JavaScript:

$(function () {
    $.get('getimage.php', function (data) { 
        $("body").html($("body").html() + data);
    });
});