2013-11-21 75 views
0

JSON數組我使用的是自動完成場從服務器獲取數據並顯示在數據表:綁定數據表,從自動完成

$(「#名字」)自動完成({

source: function (request, response) { 
     $.ajax({ 
      url: "http://myhost.com/webservices/test3.cfm", 
      data: request, 
      success: function (data) { 
        $('#results').dataTable({ 
         "bProcessing": true, 
         "bJQueryUI": true, 
         "bLengthChange": true, 
         "bFilter": true, 
         "bAutoWidth": false, 
         "bRetrieve" : true, 
         "aaData": data , 
         "aoColumns": [ 
          { "sTitle": "Name", "sName": "name" }, 
          { "sTitle": "Title", "sName": "title" }, 
          { "sTitle": "Organization", "sName": "organization" }, 
          { "sTitle": "Email", "sName": "email" }, 
          { "sTitle": "Status", "sName": "status" } 
         ] 
        }); 
      } 
     }); 
    } 

從ajax調用返回的數據爲:
[[「Steven,Grek」,「President」,「Sands Corp.」,「[email protected]」,「1」],[「Steven,Grek」 ,「聯盟」,「聯盟有限公司」,「[email protected]」,「1」],[「Steven,Grek」,「總統」,「林產品協會」,「[email protected]」,「 1「]]

我得到以下錯誤:

  • 數據表警告(表ID =「結果」):請求未知參數「1」從數據源用於行0
  • 數據表警告(表ID =「結果」 ):從供排9
  • 顯示1中的數據源要求未知參數「1」,2147 10的條目

如果我替換「aaData」:數據
自該數據的響應:
「aaData」:[[「Steven Grek」,「President」,「Sands Corp.」,「[email protected]」,「1」],[「Steven,Grek」,「Associate」,「Alliance [email protected]「,」1「],[」Steven,Grek「,」總裁「,」林產品協會「,」[email protected]「,」1「]]

它的工作原理是

任何想法我做錯了什麼?

回答

0

在同事的幫助下找出它: 來自我的ajax調用的數據類型是一個字符串。

$("#firstname").autocomplete({ 
    source: function (request, response) { 
    $.ajax({ 
     url: "http://myhost.com/webservices/test3.cfm", 
     data: request, 
     success: function (data) { 
      var obj = jQuery.parseJSON(data);  <---- typeof data is a string 
      $('#results').dataTable({ 
       "bProcessing": true, 
       "bJQueryUI": true, 
       "bLengthChange": true, 
       "bFilter": true, 
       "bAutoWidth": false, 
       "bRetrieve" : true, 
       "aaData": obj,     <---- Use the parsed json object instead 
       "aoColumns": [ 
        { "sTitle": "Name", "sName": "name" }, 
        { "sTitle": "Title", "sName": "title" }, 
        { "sTitle": "Organization", "sName": "organization" }, 
        { "sTitle": "Email", "sName": "email" }, 
        { "sTitle": "Status", "sName": "status" } 
       ] 
      }); 
     } 
    }); 
    }, 

});