2013-11-04 66 views
2

我無法解決錯誤,我經歷了許多網站上給出的所有建議。任何建議都會非常有幫助。提前致謝。DataTables警告(表id ='example'):從行0的數據源請求未知參數'clientId'

jQuery代碼:

$(document).ready(function() { 
    var client = { 
     "clientId": $('#clientId').val(), 
      "firstName": $('#firstName').val(), 
      "lastName": null 
    }; 
    var jsonReqString = JSON.stringify(client, function (key, value) { 
     if (typeof value == "number") return value; 
     if (typeof value == "boolean") return value; 
     return value == "" ? null : value; 
    }); 
    $.ajax({ 
     url: "/SimpleDT/search", 
     type: "POST", 
     data: jsonReqString, 
     dataType: "json", 
     jsonp: "cspcb", 
     contentType: "application/json", 
     timeout: 60000, 
     xhrFields: { 
      withCredentials: false 
     }, 
     beforeSend: function (xhrObj) {}, 
     success: function (retdata) { 
      alert("retdata = " + retdata); 
      //retdata ={"aaData": retdata}; 

      var result_string = JSON.stringify(retdata, function (key, value) { 
       if (typeof value == "number") return value; 
       if (typeof value == "boolean") return value; 
       if (key == 'clientId' && value == "") return "123321"; 

       return value == "" ? null : value; 
      }); 

      var objs = jQuery.parseJSON(result_string); 

      console.log("result_string data=" + result_string); 

      console.log("Obj=" + objs); 

      var lang = ""; 
      $.each(objs, function() { 
       lang += this['clientId'] + "--" + this['firstName'] + "--" + this['lastName'] + "--" + this['compName'] + "--" + this['fileNo'] + "--" + this['resiAddr1'] + "--" + this['resiAddr2'] + "--" + this['resiAddr3'] + "--" + this['city'] + "--" + this['pinCode'] + "--" + this['mobileNo1'] + "--" + this['mobileNo2'] + "--" + this['mobileNo3'] + "--" + this['submissionDate'] + "--" + this['reminderDate']; 
       console.log(lang); 
       lang = ""; 
      }); 
      $('#example').dataTable({ 
       /* "bProcessing": true, 
          "bServerSide": true, 
          "sAjaxSource": "/Spring3Hibernate4Annotation/search", 
          "sServerMethod": "POST", 
           */ 
        "aaData": "result_string", 
       /* "aoColumns" : [ { 
           "mDataProp" : "clientId", sDefaultContent: "n/a" 
          }, { 
           "mDataProp" : "firstName", sDefaultContent: "n/a" 
          }, { 
           "mDataProp" : "lastName", sDefaultContent: "n/a" 
          }, { 
           "mDataProp" : "compName", sDefaultContent: "n/a" 
          }, { 
           "mDataProp" : "fileNo", sDefaultContent: "n/a" 
          } ] */ 

        "aoColumns": [{ 
        "mDataProp": "clientId" 
       }, { 
        "mDataProp": "firstName" 
       }, { 
        "mDataProp": "lastName" 
       }, { 
        "mDataProp": "compName" 
       }, { 
        "mDataProp": "fileNo" 
       }] 
      }); 

     }, 
     error: function (jqXHR, textStatus, errorThrown) { 
      alert(
       "The following error occured in maintainance Query: " + textStatus, errorThrown); 
     } 
    }); 
}); 

HTML代碼:

<table cellpadding="0" cellspacing="0" border="0" class="display" 
    id="example"> 
    <thead> 
     <tr> 
      <th width="20%">Client Id</th> 
      <th width="25%">First Name</th> 
      <th width="25%">Last Name</th> 
      <th width="15%">Comp Name</th> 
      <th width="15%">File No</th> 
     </tr> 
    </thead> 
    <tbody> 
    </tbody> 
    <tfoot> 
     <tr> 
      <th>Client Id</th> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Comp Name</th> 
      <th>File No.</th> 
     </tr> 
    </tfoot> 
</table> 

示例:JSON resonse我從春天控制器接收:

[{ 
    "clientId": "12345", 
     "firstName": "fgh", 
     "lastName": "", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "1234", 
     "firstName": "", 
     "lastName": "", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "1234765", 
     "firstName": "XXXXX", 
     "lastName": "sdgg", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "2222222", 
     "firstName": "dghdhfjfj", 
     "lastName": "gsjdjkvbcb", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "2222222123", 
     "firstName": "dghdhfjfj", 
     "lastName": "gsjdjkvbcb", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "2312234412", 
     "firstName": "fdsffsf", 
     "lastName": "fsfggg", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "2313377", 
     "firstName": "fdsffsf", 
     "lastName": "fsfggg", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "23133772", 
     "firstName": "ASDFREW", 
     "lastName": "Bandalu", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "23133773", 
     "firstName": "SASASAS", 
     "lastName": "BDDFFDDDD", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "23133774", 
     "firstName": "SDASADDS", 
     "lastName": "DFASDFF", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "23133775", 
     "firstName": "SDASDADD", 
     "lastName": "DSADFFFDD", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "23133776", 
     "firstName": "ASSSSS", 
     "lastName": "DDDSSSS", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "23133777", 
     "firstName": "LKJKLLKK", 
     "lastName": "LPSLDFF", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "23133778", 
     "firstName": "ASDFREW", 
     "lastName": "Bandalu", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "2313389", 
     "firstName": "fdsffsf", 
     "lastName": "fsfggg", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "2344", 
     "firstName": "fdsffsf", 
     "lastName": "fsfggg", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "234412", 
     "firstName": "fdsffsf", 
     "lastName": "fsfggg", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "23441q2", 
     "firstName": "fdsffsf", 
     "lastName": "fsfggg", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "23456", 
     "firstName": "Rajiv", 
     "lastName": "Muddas", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "33333333", 
     "firstName": "Jhon", 
     "lastName": "Muddas", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "3333333333", 
     "firstName": "RA", 
     "lastName": "Gulit", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "43568", 
     "firstName": "AFSF", 
     "lastName": "ADFFGGSDF", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "444444444", 
     "firstName": "Raju", 
     "lastName": "cgdhh", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}, { 
    "clientId": "55555", 
     "firstName": "Jhon", 
     "lastName": "Muddas", 
     "compName": null, 
     "fileNo": null, 
     "resiAddr1": null, 
     "resiAddr2": null, 
     "resiAddr3": null, 
     "city": null, 
     "pinCode": null, 
     "mobileNo1": null, 
     "mobileNo2": null, 
     "mobileNo3": null, 
     "submissionDate": null, 
     "reminderDate": null 
}] 

回答

4

我認爲你需要把tr與一些td在身體所以DataTables知道在哪裏呈現ou tput的:

<tbody> 
    <tr> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td></td> 
     <td></td> 
    </tr> 
</tbody> 

我有一個類似的錯誤還當我有th元素的數目不正確相比,我的td的,所以我想這是類似的。

+0

我想你的建議的解決方案,但沒有luck.Table是越來越裝有靜態JSON正確的,只有當我的JSON響應分配給表不加載錯誤。 – user2953167

2

您正在將aaData設置爲字符串"result_string",您希望將其設置爲對象retdata(至少看起來是這樣)。

"aaData": retdata, 

另外aaData應該是一個數組的數組,但是對象的陣列可工作。當您使用sAjaxSource時,通常會使用mDataProp

如果使用sAjaxSource你也許可以做以下的,而不是你的Ajax調用

$('#example').dataTable({ 
     "sAjaxSource": "/SimpleDT/search", 
     "aoColumns": [{ "mDataProp": "clientId" }, 
        { "mDataProp": "firstName" }, 
        { "mDataProp": "lastName" }, 
        { "mDataProp": "compName" }, 
        { "mDataProp": "fileNo" }], 
     "fnServerData": function(sUrl, aoData, fnCallback, oSettings) { 
     oSettings.jqXHR = $.ajax({ 
      url: sUrl, 
      type: "POST", 
      data: jsonReqString, 
      dataType: "json", 
      jsonp: "cspcb", 
      contentType: "application/json", 
      timeout: 60000, 
      xhrFields: { withCredentials: false } 
      cache: false 
     }); 
     } 
}); 
+0

你可以將它設置爲'retdata'來獲得原始的JSON或'objs'來獲得JSON修改後的JSON.stringify兩個似乎工作 –

+0

HI Danny,我試過你的建議的解決方案,但沒有運氣這次數據表可以不加載。有沒有其他辦法可以解決這個問題。如果我嘗試提供硬編碼的json響應,我從後端收到的表格中獲取並正確顯示記錄,我嘗試驗證JSON及其驗證。根據我的理解,沒有任何問題會出現什麼問題無法識別:(。 – user2953167

相關問題