2015-11-12 19 views
-2

無法弄清楚如何真正解決這個問題。我在getJson方法之前和之後給控制檯打印了一行,並且只能獲得之前的版本。

這是我收到的控制檯上的錯誤:

我已經一團糟的路徑很多,這似乎並不成爲問題,我已驗證我的JSON是準確的。這裏是我的JavaScript代碼

$(document).ready(function() { 
    var myList; 
    console.log("here"); 
    $.getJSON('/vegetables.json').done(function (data) { 
     console.log("also here"); 
     myList = data; 
     //console.log(myList['vegetables'][0].name); 

     var uList = document.getElementById("items"); 
     for (i = 0; i < myList['vegetables'].length; i++) { 

      var div = document.createElement('div'); 
      div.setAttribute('class', 'col-sm-3 vegetables'); 

      var name = document.createElement('h3'); 
      name.innerHTML = myList['vegetables'][i].name; 
      div.appendChild(name); 

      var description = document.createElement('p'); 
      description.innerHTML = myList['vegetables'][i].description; 
      div.appendChild(description); 

      var price = document.createElement('p'); 
      price.innerHTML = myList['vegetables'][i].price; 
      div.appendChild(price); 

      var addToCart = document.createElement('button'); 
      addToCart.setAttribute('value', i); 
      addToCart.setAttribute('class', 'btn btn-primary'); 
      addToCart.innerHTML = "Add to Cart <img src='glyphicons-203-shopping-cart.png' />"; 
      addToCart.setAttribute('onClick', 'carrot(this)'); 
      div.appendChild(addToCart); 

      /*<div class="col-sm-3 vegetables" > 

        <h3 id="div1Name"></h3> 
        <p id="div1Description">Carrots are orange </p> 
        <p id="div1Price"></p> 
        <button onclick="carrot(this)" type="button" class="btn btn-primary" />Add to Cart <img src="glyphicons-203-shopping-cart.png" /> 
       </div>*/ 

      //li.appendChild(panel); 
      uList.appendChild(div); 
     } 
    }); 
    }); 

頭標籤jQuery的

<head> 
<title>Vegetable</title> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="main.css"> 
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 

讓我知道你什麼都將有所幫助。

+0

嘗試使用您的瀏覽器查詢該URL。如果JSON響應出現語法錯誤,'getJson'將靜默失敗。 –

+0

這是網絡(HTTP)錯誤,與您的JavaScript代碼無關。 404狀態只是說文件不存在。 – undefined

回答

0

它正在尋找你的服務器地址的文件your_server_addr/vegetables.json(EX localhost:8080/vegetables.json),並且該文件沒有在這個位置上。

相關問題