JSON data from server could not be parsed.

JSON data from server could not be parsed.

SimonshengSimonsheng Posts: 10Questions: 0Answers: 0
edited July 2012 in General
I am using asp to return data from oracle database. I add fields by add strings like
String aa = "{aaData:[ ["field1","field2"], ["field1","field2"], ....["field1","field2"]]}".
my database and data access asp are working very well.(I can retrive all data that I need)
but my datatables give me error: JSON data from server could not be parsed.

dataTable: in page Default.asp
[code]
<!DOCTYPE HTML>







$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "newUniqueReportDataAjax.asp"
} );
} );






CREATION DATE
USERNAME
HEADERID
FEESCHEDULENAME
PRODUCT
DEFAULT PERCENT
EFFECTIVE DATE
TERMINATION DATE




Loading data from server




CREATION DATE
USERNAME
HEADERID
FEESCHEDULENAME
PRODUCT
DEFAULT PERCENT
EFFECTIVE DATE
TERMINATION DATE





[/code]
data access in newUniqueReportDataAjax.asp
[code]
<!--#include file="adojavas.inc"-->



<%
var conString = "Provider =" + Application("appProvider") + ";" +
"User ID =" + Application("appOwner") + ";" +
"Password =" + Application("appPassword") + ";" +
"Data Source =" + Application("appDSN") + ";" +
"Persist Security Info=" + Application("appPersistSecurityInfo");
var conn = Server.CreateObject("ADODB.Connection");
var cmd = Server.CreateObject("ADODB.Command");
var rs = Server.CreateObject("ADODB.Recordset");
cmd.CommandText = "{call page.spReportNewUNIQUEMulti(?,?,?,?,?,?,?)}";
cmd.CommandType = adCmdText;
var param1 = cmd.CreateParameter("PID_STARTDATE",adDBDate,adParamInput,10,"12-JUL-88");
var param2 = cmd.CreateParameter("PID_ENDDATE",adDBDate,adParamInput,255,"12-JUL-25");
var param3 = cmd.CreateParameter("PIV_SCHEDULECODE",adVarChar,adParamInput,255,null);
var param4 = cmd.CreateParameter("PIV_USERIDS",adVarChar,adParamInput,255,null);
var param5 = cmd.CreateParameter("PG_LENGTH",adBigInt,adParamInput,5000,500);
var param6 = cmd.CreateParameter("PG_PAGE",adBigInt,adParamInput,5000,1);
var param7 = cmd.CreateParameter("PG_TOTAL",adBigInt,adParamOutput,5000,null);
cmd.Parameters.Append(param1);
cmd.Parameters.Append(param2);
cmd.Parameters.Append(param3);
cmd.Parameters.Append(param4);
cmd.Parameters.Append(param5);
cmd.Parameters.Append(param6);
cmd.Parameters.Append(param7);
conn.Open(conString);
cmd.ActiveConnection = conn;
try{
rs = cmd.Execute();
}catch(e){
Response.Write(e.message);
}
var rsString = '{ "aaData" : [';
while(!rs.EOF){
rsString = rsString + '[';
rsString = rsString + '"' + rs.Fields("CreationDate")+'",'
rsString = rsString + '"' + rs.Fields("CreatedBy")+'",'
rsString = rsString + '"' + rs.Fields("ScheduleCode")+'",'
rsString = rsString + '"' + rs.Fields("ScheduleName")+'",'
rsString = rsString + '"' + rs.Fields("ProductType")+'",'
rsString = rsString + '"' + rs.Fields("Percentage")+'",'
rsString = rsString + '"' + rs.Fields("EffectiveDate")+'",'
rsString = rsString + '"' + rs.Fields("TerminationDate")+'"'
rsString = rsString + ']';
rs.MoveNext();
if (!rs.EOF)
{
rsString = rsString + ',';
}
}
var rsString = rsString + ']}';
cmd = null;
conn.Close();
conn = null;
Response.Write(rsString);
%>


[/code]

If I call newUniqueReportDataAjax.asp directly, I will get a jason string:
[code]
{ "aaData":[
["07/12/2012 15:46:26","aaaa","FSIMONTEST","simon data test","HMA","9","09/06/1999","12/31/9999"],
["07/12/2012 17:26:34","aaaa","FTT","ttt","PRIMARY","4","07/11/2000","12/31/9999"],
["07/12/2012 17:26:42","aaaa","FTTT","tttt","PRIMARY","4","07/11/2000","12/31/9999"],
["07/12/2012 17:26:47","aaaa","FTTTT","ttttt","PRIMARY","4","07/11/2000","12/31/9999"],
["07/12/2012 17:26:51","aaaa","FTTTTT","tttttt","PRIMARY","4","07/11/2000","12/31/9999"],
["07/12/2012 17:26:57","aaaa","FTTTTTT","ttttttt","PRIMARY","4","07/11/2000","12/31/9999"],
["07/12/2012 17:27:02","aaaaNG","FTTTTTTT","tttttttt","PRIMARY","4","07/11/2000","12/31/9999"],
["07/12/2012 17:27:06","aaaa","FTTTTTTTT","ttttttttt","PRIMARY","4","07/11/2000","12/31/9999"],
["07/12/2012 17:27:10","aaaa","FTTTTTTTTT","tttttttttt","PRIMARY","4","07/11/2000","12/31/9999"],
["07/12/2012 17:27:14","aaaa","FTTTTTTTTTT","ttttttttttt","PRIMARY","4","07/11/2000","12/31/9999"],
["07/12/2012 17:27:20","aaaa","FTTTTTTTTTTT","tttttttttttt","PRIMARY","4","07/11/2000","12/31/9999"],
["07/09/2012 17:10:20","aaaa","HGEORGE","GEORGE HMA SCHEDULE","HMA","69","01/01/1970","12/31/9999"],
["07/13/2012 14:56:07","KELLY.aaaa","FKELLYTEST","test","HMA","100","01/01/2012","12/31/9999"],
["07/13/2012 14:56:29","KELLY.aaaa","FKELLYTEST1","test","HMA","100","01/01/2012","12/31/9999"],
["07/13/2012 14:56:43","KELLY.MaaaaA","FKELLYTEST2","test","HMA","100","01/01/2012","12/31/9999"],
["07/13/2012 14:56:53","KELLY.aaaa","FKELLYTEST3","test","HMA","100","01/01/2012","12/31/9999"],
["07/13/2012 14:56:59","KELLY.aaaa","FKELLYTEST4","test","HMA","100","01/01/2012","12/31/9999"],
["07/13/2012 14:57:09","KELLY.MaaaaA","FKELLYTEST5","test","HMA","100","01/01/2012","12/31/9999"],
["07/13/2012 14:57:15","KELLY.aaaa","FKELLYTEST6","test","HMA","100","01/01/2012","12/31/9999"]
]}
[/code]

if I save them as a text file, and set datatable ajaxdatasource to the text file's name. it is working very well.

Replies

  • SimonshengSimonsheng Posts: 10Questions: 0Answers: 0
    Waiting online for help please
  • rgvcorleyrgvcorley Posts: 29Questions: 0Answers: 0
    Have you checked in the console that what is being returned in the ajax call is the same as what works in the static text file?
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Run your table through the DataTables debugger and let us know the unique code you get.

    However as rgvcorley says, the Ajax return will be fundamental to the issue. If DataTables says it isn't valid JSON, then its because the JSON isn't valid :-)

    Allan
  • SimonshengSimonsheng Posts: 10Questions: 0Answers: 0
    edited July 2012
    I checked by alert, asp return a web page, the return is not only json, it is a json in html
    [code]

    ........

    json.......


    [/code]
    I am tring change Response.ContentType to "application/json", it doesn't work, I know where is the problem,but I don't know how to fix it. dataTable receive ajax datatype is "json",but this response text/html.
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Regardless of what you set the content type to - if it doesn't return valid JSON, then it isn't going to work :-). You need to find a way of getting the server to return just valid JSON - however that might be done in the server platform you are using.

    Allan
  • SimonshengSimonsheng Posts: 10Questions: 0Answers: 0
    I got it. delete all html code in ajax asp page.
    data access in newUniqueReportDataAjax.asp
    [code]
    <% @language="javascript";
    %>
    <!--#include file="adojavas.inc"-->
    <%
    var conString = "Provider =" + Application("appProvider") + ";" +
    "User ID =" + Application("appOwner") + ";" +
    "Password =" + Application("appPassword") + ";" +
    "Data Source =" + Application("appDSN") + ";" +
    "Persist Security Info=" + Application("appPersistSecurityInfo");
    var conn = Server.CreateObject("ADODB.Connection");
    var cmd = Server.CreateObject("ADODB.Command");
    var rs = Server.CreateObject("ADODB.Recordset");
    cmd.CommandText = "{call PKGSCHEDULE_UTIL.spReportNewUNIQUEMulti(?,?,?,?,?,?,?)}";
    cmd.CommandType = adCmdText;
    var param1 = cmd.CreateParameter("PID_STARTDATE",adDBDate,adParamInput,10,"12-JUL-88");
    var param2 = cmd.CreateParameter("PID_ENDDATE",adDBDate,adParamInput,255,"12-JUL-25");
    var param3 = cmd.CreateParameter("PIV_SCHEDULECODE",adVarChar,adParamInput,255,null);
    var param4 = cmd.CreateParameter("PIV_USERIDS",adVarChar,adParamInput,255,null);
    var param5 = cmd.CreateParameter("PG_LENGTH",adBigInt,adParamInput,5000,500);
    var param6 = cmd.CreateParameter("PG_PAGE",adBigInt,adParamInput,5000,1);
    var param7 = cmd.CreateParameter("PG_TOTAL",adBigInt,adParamOutput,5000,null);
    cmd.Parameters.Append(param1);
    cmd.Parameters.Append(param2);
    cmd.Parameters.Append(param3);
    cmd.Parameters.Append(param4);
    cmd.Parameters.Append(param5);
    cmd.Parameters.Append(param6);
    cmd.Parameters.Append(param7);
    conn.Open(conString);
    cmd.ActiveConnection = conn;
    try{
    rs = cmd.Execute();
    }catch(e){
    Response.Write(e.message);
    }
    var rsString = '{ "aaData" : [';
    while(!rs.EOF){
    rsString = rsString + '[';
    rsString = rsString + '"' + rs.Fields("CreationDate")+'",'
    rsString = rsString + '"' + rs.Fields("CreatedBy")+'",'
    rsString = rsString + '"' + rs.Fields("ScheduleCode")+'",'
    rsString = rsString + '"' + rs.Fields("ScheduleName")+'",'
    rsString = rsString + '"' + rs.Fields("ProductType")+'",'
    rsString = rsString + '"' + rs.Fields("Percentage")+'",'
    rsString = rsString + '"' + rs.Fields("EffectiveDate")+'",'
    rsString = rsString + '"' + rs.Fields("TerminationDate")+'"'
    rsString = rsString + ']';
    rs.MoveNext();
    if (!rs.EOF)
    {
    rsString = rsString + ',';
    }
    }
    var rsString = rsString + ']}';
    cmd = null;
    conn.Close();
    conn = null;
    Response.Write(rsString);
    %>
    [/code]
This discussion has been closed.