簡體   English   中英

Ajax Image Preloader不起作用

[英]Ajax Image Preloader doesn't work

我正在使用Ajax制作預加載器,但它似乎無法工作。 我想加載一個圖像,如果它在那里,成功做了一些事情 - 但是如果它不存在只是錯誤做另一件事 - 另一個文本 - 但是在這個代碼中如果找到圖像無關緊要或者不是,無論發生什么都發生警報,​​我的意思是如果發現圖像都發生了警報,但是如果我刪除了照片,那么兩個警報也會發生,那么用ajax完成和錯誤來實現這些人的方法是什么? 感謝您提供此指南中的任何指南。

function preloadImages()
{   
 var $image = 'http://www.mysite/images/example.jpg';
 jQuery.ajax({
    url: $image,
            complete: function(){
         fileLoaded();
     },
    error: function(){
        errorLoading();
    }
 });    
}

function fileLoaded(){
alert('There is the image');
}

function errorLoading(){
alert('No image');
}

在此先感謝您的幫助。 問候。

改變complete success

jQuery.ajax({
 url: $image,
     success: function(){
     fileLoaded();
 },
error: function(){
    errorLoading();
}
});

小提琴

試着找出錯誤,就像在這個SO 答案中一樣

error: function(xhr, status, error) {
  var err = eval("(" + xhr.responseText + ")");
  alert(err.Message);
}

如果圖像位於站點目錄中,請按以下方式執行

    var image = '../Images/Slider/1.jpg'; // give in the src of the image
    $.ajax({
        type: 'GET',
        url: image,
        success: function (bytes) {
            alert('in');
        },
        error: function () {
            alert('Error!!!')
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM