2014-02-06 90 views
0

的特性「長度」我已經下載了以下web-based-face-detect-program並編譯它,我的編譯器顯示沒有語法錯誤,但是當我嘗試上傳與該應用程序我得到一個Cannot read property 'length' of undefined error圖片。我根本沒有改變應用程序,所以不應該有任何問題,我對JavaScript很新,所以我可能會犯一個新手的錯誤。繼承人js功能代碼:無法讀取未定義的錯誤

function parse_response(res) { 
    var data = jQuery.parseJSON(res); 

    if (data.success == false) { 
     show_page(form); 
     show_message(data.msg); 
     return; 
    } 

    results 
     .find(".source-img").html("").end() 
     .find(".detected-faces").html("").append("<p>Found " + (data.images.length-1) + " faces:</p>"); 

    $.each(data.images, function(i, img) { 
     var el = '<img src="face-detect/' + img.src + '?r=' + Math.random() + '" alt="" />'; 
     results.find((i == 0 ? ".source-img" : ".detected-faces")).append(el); 
    }) 

    results.find(".source-img img").bind('click', function(e) { 
     var im = data.images[0]; 
     var el = '<img src="face-detect/' + im.src + '" width="' + im.width + '" height="' + im.height + '" alt="" />'; 
     $(".lightbox", results).find(".lightbox-content").html(el).end().lightbox("show"); 
    }); 

    show_page(results, actions); 
} 

.find(".detected-faces").html("").append("<p>Found " + (data.images.length-1) + " faces:</p>");是導致錯誤的原因。和繼承人的錯誤,我得到在Chrome控制檯:

Uncaught TypeError: Cannot read property 'length' of undefined app.js:87 
parse_response app.js:87 
xhr.onreadystatechange app.js:65 

代碼的其餘部分是所有GitHub的鏈接,如果有件事我已經錯過了。那麼有人可以向我解釋爲什麼我得到這個未定義的錯誤,以及我如何解決它?

+1

這意味着響應沒有 「圖像」 屬性。使用瀏覽器開發者控制檯檢查實際返回的JSON。 – Pointy

+0

@Pointy我該怎麼做?我認爲這是問題,但因爲它不是我的代碼,我認爲它是在JSON中。 – user3144542

+0

如果您使用的是Chrome,請查看「網絡」選項卡。您應該能夠單擊涉及的HTTP事務並查看標題和實際響應。 – Pointy

回答

0

看來數據對象沒有圖像屬性。在錯誤行之前,使用JSON.stringify(data)函數檢查數據對象結構。

您可以使用:

console.log(JSON.stringify(data)) 

或者:

alert(JSON.stringify(data)) 
+0

這兩個都沒有顯示JSON輸出給我,'alert'是得到這個錯誤'未捕獲的ReferenceError:警報未定義'並且控制檯也不顯示任何東西,具有相同的錯誤。 – user3144542

相關問題