2012-12-22 87 views
2

我在Visual Studio 2012是新的,我有一個項目,我需要一些圖像加載到通過JavaScript一看,基本上我都充滿了一些項目的描述和每個路徑的數組圖像,但是當頁面加載頁面不能因爲它從來沒有發現他們加載圖像問題加載圖像2012

我用來實現這一目標的JavaScript代碼:

 var item = { 
     name: "", 
     price: 0, 
     index: 0, 
     picURL: "" 
    }; 

    function Item(name, price, index, picURL) { 
     this.name = name; 
     this.price = price; 
     this.index = index; 
     this.picURL = picURL; 
    } 

    function FillArray() { 

     var items = new Array(); 

     items[0] = new Item("Gnome1", 32, 001, "Content/img/gnome7.PNG"); 
     items[1] = new Item("Gnome2", 15, 002, "Content/img/gnome14.PNG"); 
     items[2] = new Item("Gnome3", 26, 003, "Content/img/gnome10.PNG"); 

     return items; 
    } 


    function addToCart() { 
     var items = FillArray(); 
     var that = this; 
     var ul = document.getElementById("list"); 
     for (var i = 0; i < items.length; i++) { 

      var li = document.createElement("li"); 
      li.id = 'li' + items[i].index; 
      var currentItem = items[i]; 

      for (p in currentItem) { 
       var div = document.createElement("div"); 
       if (p.valueOf() == "picURL") { 
        div.innerHTML = "<img class='miniPic' src='" + currentItem[p].valueOf() + "'/>"; 
       } 
       else if (p.valueOf() == "price") { 
        div.innerHTML = p.valueOf() + " : £" + currentItem[p].valueOf(); 
       } 
       else { 
        div.innerHTML = p.valueOf() + " : " + currentItem[p].valueOf(); 
       } 
       li.appendChild(div); 
      } 
} 

但是,當我調用視圖此答案是我得到:

GET /Home/Content/img/gnome7.PNG 404(找不到)

我試圖改變文件的位置,路徑,但似乎沒有任何工作,這是同樣的情況,當我嘗試在我的項目中本地調用json文件,它也說我無法找到,所以我認爲它可能是我的視覺工作室的一種配置,但我無法使其工作,有人可以幫我嗎?這是很煩人:(

回答

1

我的猜測是,你還沒有包括圖像作爲解決方案的一部分,因此他們沒有得到部署的解決方案。

爲了解決這個問題,轉到Solution Explorer並導航到Content -> Img文件夾(確保在解決方案資源管理器頂部的顯示所有文件按鈕被選中)
在img文件夾中,您應該看到gnomeXX圖像,如果它們是灰色的,請右鍵單擊他們選擇Include in Project

嘗試建設和再次運行。

+0

非常感謝您的回答@Blachshma,是的,這些文件包含在解決方案中,並且我已經完成了該建築並再次運行,但仍未找到圖像 – cacharry

+0

您輸入的URL是什麼瀏覽器?因爲我看到你試圖去'/首頁/內容/ IMG/gnome7.PNG'而不是'/Content/img/gnome7.PNG 404' - 這可能是問題... – Blachshma

0

花了5分鐘的寶貴時間在此之前,我實現了明顯的...嘗試Ctrl + F5在瀏覽器上......可能是一個靜態的內容/緩存問題。

您還需要確保根據相關文件的「屬性」構建/內容設置是一樣的正確引用您的其他資源。