2012-12-22 84 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在浏览器上......可能是一个静态的内容/缓存问题。

您还需要确保根据相关文件的“属性”构建/内容设置是一样的正确引用您的其他资源。