2017-02-03 450 views
0

我需要過濾一些信息。這些信息來自JSON解析。我無法讓它工作。我想要的是JSON被過濾到HTML類中。我以爲我傻啊JavaScript解析失敗JSON

$.ajax({ 
    // Agenda 
    type: 'POST', 
    url: 'agendas', 
    data: {results: 'events'}, 
    dataType: 'json', 
    cache: false, 
    success: function (response) { 
     $('.date, .country, .events').html(''); 
     $.each(response.results, function (index, result) { 
      if (result.status) 
       $('.date').append(result.server); 
       $('.country').append(result.server); 
       $('.events').append(result.server); 


     }); 

    } 
}); 

這將是可愛的,如果有人可以幫助我

JSON:

{ 
"results": [ 
    { 
     "events": { 
      "id": 1, 
      "date": "2022-05-06T00:00:00+00:00", 
      "description": "test", 
      "time": "2017-02-03T06:40:00+00:00", 
      "location": "NL", 
      "year": "2008", 
      "event": "Idk" 
     } 
    }, 
    { 
     "events": { 
      "id": 2, 
      "date": "2019-04-05T00:00:00+00:00", 
      "description": "aasdasdasda", 
      "time": "2017-02-03T15:04:00+00:00", 
      "location": "asdasdasd", 
      "year": "0000", 
      "event": "asdasd" 
     } 
    } 
] 
} 

CSS:

<div class="day"> 
       <h2 class="date">Januari 23</h2> 
       <div class="country-events"> 
        <span class="country">UK</span> 
        <div class="events"> 
         <span class="event">Conference Amsterdam<br />11:00 CET</span> 
         <span class="event">Webinar Copenhagen<br />15:00 CET</span> 
        </div> 
       </div> 

我真的不能得到我的頭在這附近。我知道這不是正確的代碼,我是JSON和JavaScript的新手。目前第4天請原諒我。

預先感謝您

UPDATE

This is what it shows currently, the data from the json file needs to go in the specific elements

+1

不是說而是「*我不能讓它工作*」,試圖解釋究竟是什麼* *與代碼腳麻您發佈。它錯誤嗎?它產生意想不到的輸出嗎?如果他們知道你的問題是什麼,人們將能夠更好地提供幫助。 –

+2

您的'結果'對象沒有'status'屬性。 – evolutionxbox

+1

@evolutionxbox和'server'。 –

回答

1
$.each(response.results, function(index, result) { 
    console.log(result) 
    $('.date').append(result.events.date); 
    $('.date').append('</br>'); 
    $('.country').append(result.events.location); 
    $('.country').append('</br>'); 
    $('.events').append(result.events.event); 
    $('.events').append('</br>'); 

}); 

這是你想要的嗎?

https://plnkr.co/edit/wCYJPXgAPII1mcW4cKw7?p=preview

檢查此琴

+0

我試過了,它仍然沒有在我的儀表板上顯示任何東西 –

+0

'response.results.date'未定義,請參閱他提供的JSON文件! –

+0

嘗試小提琴,它基本上應該是什麼,在你的成功方法 –