2016-08-05 57 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