2016-08-05 55 views
-1

我嘗試將數據發送到數據表中的JSON文件中像波紋管的每個值:這是我的功能如何從JSON文件

"responseHeader":{ 
    "status":0, 
    "QTime":2, 
    "params":{ 
     "q":"vba", 
     "indent":"true", 
     "fl":"name,role_t,born,natio_t", 
     "wt":"json"}}, 
    "response":{"numFound":7,"start":0,"docs":[ 
     { 
     "name":"Khouli", 
     "born":["1978-04-03T00:00:00Z"], 
     "natio_t":"tunisien", 
     "role_t":"Consultant"}, 
     { 
     "name":"Atayi", 
     "born":["1987-06-24T00:00:00Z"], 
     "natio_t":"Francaise", 
     "role_t":"Consultant"} 
} 

$ (document).ready(function() { 
$.ajax({ 
    type: 'GET', 
    url: '../search.json', 
    success: function(data) { 
     $.each(data, function(i, data) { 
      var body = "<tr>"; 
      body += "<td>" + data.name + "</td>"; 
      body += "<td>" + data.born + "</td>"; 
      body += "<td>" + data.natio_t + "</td>"; 
      body += "<td>" + data.role_t + "</td>"; 

      body += "</tr>"; 
      $('.datatable-ajax-source table').append(body); 
     }); 

我的JSON文件的 爲例數據但我得到表的未分配值 this is my table result

如何從json文件中獲取此值

+0

您的JSON數據以不同的方式無效。請張貼有效的JSON。 – 2016-08-05 14:56:33

+0

顯然數據不是數組。爲什麼你會像一個超越我一樣對待它。 –

回答

0

輸入數據似有不妥:這裏修正後的數據:

{ 
"responseHeader":{ 
    "status":0, 
    "QTime":2, 
    "params":{ 
     "q":"vba", 
     "indent":"true", 
     "fl":"name,role_t,born,natio_t", 
     "wt":"json"}}, 
     "response":{"numFound":7,"start":0,"docs":[ 
     { 
     "name":"Khouli", 
     "born":["1978-04-03T00:00:00Z"], 
     "natio_t":"tunisien", 
     "role_t":"Consultant"}, 
     { 
     "name":"Atayi", 
     "born":["1987-06-24T00:00:00Z"], 
     "natio_t":"Francaise", 
     "role_t":"Consultant"} 
    ] 
} 
} 

的適當的JS來處理這個問題:

$(document).ready(function() { 
$.ajax({ 
type: 'GET', 
url: '../search.json', 
success: function(data) { 
    // Data is the root node of your json response. 
    // You need to navigate through the "docs" node. 
    if(data.response.docs && data.response.docs.length > 0) 
    { 
    $.each(data.response.docs, function(i, item) { 
     var body = "<tr>"; 

     body += "<td>" + item.name + "</td>"; 
     body += "<td>" + item.born + "</td>"; 
     body += "<td>" + item.natio_t + "</td>"; 
     body += "<td>" + item.role_t + "</td>"; 

     body += "</tr>"; 
     $('.datatable-ajax-source table').append(body); 
    } 
    }); 

你沒有得到FO正確的值r data。 您應該得到docs.data[i],在這種情況下,item.each()中定義。

+0

謝謝@DidierAupest你的答案,因爲但是,當我嘗試這個解決方案,我得到這個錯誤jquery.min.js:4 Uncaught TypeError:無法讀取undefinedeach @ jquery.min.js的屬性'長度':4success @ application.js: 111c @ jquery.min.js:4fireWith @ jquery.min.js:4k @ jquery.min.js:6r @ jquery.min.js:6 – MedKostali

+0

我已更新答案。 ;) –

+0

它仍然是相同的錯誤application.js:116 Uncaught TypeError:無法讀取此ligne中未定義的屬性「長度」if(data.response.docs && data.docs.length> 0) – MedKostali

-2

你沒有告訴jquery你期待json回來,所以它從服務器的響應接受JSON字符串作爲明文,並以純文本的形式傳遞給data

要麼使用.getJSON(),這將自動解析響應爲本地JS數據結構或

$.ajax(
    ... 
    dataType: "json" 
    ... 
); 
+0

的確,這可能是問題的一部分,但是如果服務器正確設置了Content-Type頭,jQuery應該解析它。無論如何,從我所知道的情況來看,他需要更深入地探究目前無效的結構。 – 2016-08-05 14:59:34