Getting Error : Json data from server could not be parsed. This is caused by JSON formatting error.

Getting Error : Json data from server could not be parsed. This is caused by JSON formatting error.

rajneeshkmrrajneeshkmr Posts: 3Questions: 0Answers: 0
edited May 2013 in DataTables 1.9
Hi ,
I am using SQL server 2005 at my BackEnd and trying to retrieve data from server in the form of JSON. i validated the JSON and it's Valid. When i paste the JSON in file and try to access it, it's working fine.
Please find Below the code.
$(document).ready(function () {
// $.fn.dataTableExt.sErrMode = 'throw';
$('#testTable').dataTable({ "sPaginationType": "full_numbers", "bJQueryUI": true, "bProcessing": true, "bServerSide": true,
// "sAjaxSource": "/SupportApp/1.txt",
"sAjaxSource": "/SupportApp/Default.aspx/FetchFOIDetails",
"aoColumns": [
{ "mData": "ServiceOrderNo" },
{ "mData": "PcodeDesc" },
{ "mData": "Action" },
{ "mData": "Status" }
],
},
"error": function () { alert('error'); }
});

Server side code is :
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = true)]
//public static string FetchFOIDetails(string TaskID)
public static string FetchFOIDetails()
{
string str = "{\"aaData\" : [{\"ServiceOrderNo\":\"511121336727696846-3\",\"PcodeDesc\":\"Configure Admin Account in AccMan\",\"Action\":\"Cease\",\"Status\":\"CLOSE\"},{\"ServiceOrderNo\":\"511121336727696862-3\",\"PcodeDesc\":\"Configure Admin Account in AccMan for Toolkit\",\"Action\":\"Cease\",\"Status\":\"CLOSE\"},{\"ServiceOrderNo\":\"511121336727696866-8\",\"PcodeDesc\":\"Configure Admin Account in AccMan for Toolkit Access\",\"Action\":\"Cease\",\"Status\":\"CLOSE\"}]}";
return str;
}


Please help me!!!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    The error is exactly what it says it is. The reply form the server is not valid JSON.

    Also please link a test case in future: http://datatables.net/forums/discussion/12899/post-test-cases-when-asking-for-help-please-read

    Allan
  • rajneeshkmrrajneeshkmr Posts: 3Questions: 0Answers: 0
    Hi Allan,
    Thanks for your quick response.

    Please find below the test case:

    $(document).ready(function () {
    // $.fn.dataTableExt.sErrMode = 'throw';
    $('#testTable').dataTable({ "sPaginationType": "full_numbers", "bJQueryUI": true, "bProcessing": true, "bServerSide": true,
    // "sAjaxSource": "/SupportApp/1.txt",
    "sAjaxSource": "/SupportApp/Default.aspx/FetchFOIDetails",
    "error": function () { alert('error'); },
    "aoColumns": [
    { "mData": "ServiceOrderNo" },
    { "mData": "PcodeDesc" },
    { "mData": "Action" },
    { "mData": "Status" }
    ]}

    );

    $("#testTable tbody").delegate("tr", "click", function () {
    var firstCellText = $("td:first", this).text();
    var fourthCellText = $("td:eq(3)", this).text();
    alert(firstCellText + " " + fourthCellText);
    });
    });



    I also validated my JSON on http://jsonlint.com and it's valid .
    The JSON is :

    {"aaData" : [{"ServiceOrderNo":"511121336727696846-3","PcodeDesc":"Configure Admin Account in AccMan","Action":"Cease","Status":"CLOSE"},{"ServiceOrderNo":"511121336727696862-3","PcodeDesc":"Configure Admin Account in AccMan for Toolkit","Action":"Cease","Status":"CLOSE"},{"ServiceOrderNo":"511121336727696866-8","PcodeDesc":"Configure Admin Account in AccMan for Toolkit Access","Action":"Cease","Status":"CLOSE"}]}

    i put the breakpoint on serverside method, but it's not hitting the breakpoint.
    Can you please suggest me ??
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    There much be something else in the return making it invalid. That warning is coming from jQuery, not DataTables. Please link to a live test case or use the DataTables debugger so we can see a trace.

    Allan
  • rajneeshkmrrajneeshkmr Posts: 3Questions: 0Answers: 0
    Hi Allen,
    Thanks for your response again. i was able to trace the issue.
    "contentType": "application/json; charset=utf-8" was missing in my AJAX call. i am able to get a call for the method now.
    But ,oncre the JSON is returned, i am getting error "Microsoft JScript runtime error: Unable to get value of the property 'length': object is null or undefined" in jquery.datatables.js.
    My JSON Response is as follows:

    {"iTotalRecords":6,"iTotalDisplayRecords":6 ,"aaData" : [{"ServiceOrderNo":"IBTAAICIADS25558880-11","PcodeDesc":"Confirm Order Closure for ADS","Action":"Provide","Status":"CLOSE"},{"ServiceOrderNo":"IBTAAICIApmo25558880-13","PcodeDesc":"Confirm Order Closure (AIntl-APMo)","Action":"Provide","Status":"CLOSE"},{"ServiceOrderNo":"IBTAAICIEB25558880-11","PcodeDesc":"Confirm Order Closure for Web Analysis Base (CAS)","Action":"Provide","Status":"CLOSE"},{"ServiceOrderNo":"IBTAAICIUD25558880-11","PcodeDesc":"Confirm Order Closure for Unified Dashboard (VSM)","Action":"Provide","Status":"CLOSE"},{"ServiceOrderNo":"IBTAAICIVB25558880-7","PcodeDesc":"Confirm Order Closure for Vantage Base","Action":"Provide","Status":"CLOSE"},{"ServiceOrderNo":"IBTAAICIWA25558880-11","PcodeDesc":"Confirm Order Closure for Web Analysis (CAS)","Action":"Provide","Status":"CLOSE"}]}

    Can you please help me OR atleast suggest, where should i look for ?
    I have googled alot , but not finding any convincing solution.
    I am able to read the data ,when i keep data in file and i can't replicate it, as the JSON is being returned from Webmethod.

    I have checked JSON on http://pro.jsonlint.com/ and it's valid.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    It does indeed look a valid and correct. My only guess is that it is being seen as a string (i've seen the JSON return from some MS server's being wrapped up as a string) - but that's really a guess. I honestly can't say or offer much help without being able to see the problem - that's why I keep asking for test cases. So I can actually offer some help rather than just guessing!

    Allan
This discussion has been closed.