Asmx web service (framework 3.5) not returning proper format JSON?

Asmx web service (framework 3.5) not returning proper format JSON?

cmanvacmanva Posts: 37Questions: 0Answers: 0
edited April 2014 in General
The JSON returned is incorrect format. Anyone else using web services to return JSON?

[code]
{"d":"[{\"Name\":\"AAA\",\"Company\":\"AAAA\",\"Address\":\"AAAA\",\"Phone\":\"1204675\",\"Country\":\"US\"},{\"Name\":\"AAAA\",\"Company\":\"AAAA\",\"Address\":\"AAAA\",\"Phone\":\"AAA\",\"Country\":\"AAA\"}]"}
[/code]

My Javascript:
[code]
var oTable = $('#auditList').dataTable({
"bServerSide": true,
"sAjaxSource": "Services/HCTCService.asmx/TestJSON",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "POST",
"data": "{}",
"url": sSource,
"contentType": "application/json; charset=utf-8",
"success": fnCallback,
"error": handleAjaxException // this sets up jQuery to give me errors
} );

},
"oLanguage": { "sZeroRecords": "No audits available", "sEmptyTable": "No audits available" },
"bProcessing": true,
"bDeferRender": true,
"iDisplayLength": 5,
"bLengthChange": false,
"bDestroy": true,
"bFilter": false,
"aoColumns": [
{ "sName": "Name" },
{ "sName": "Company" },
{ "sName": "Address"},
{ "sName": "Phone" },
{ "sName": "Country"}
],
"sPaginationType": "full_numbers"
});
}
[/code]

My webservices:

[code]
[System.Web.Script.Services.ScriptService]
public class HCTCService : System.Web.Services.WebService
{

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string TestJSON()
{
Employee[] e = new Employee[2];
e[0] = new Employee();
e[0].Name = "AAA";
e[0].Company = "AAAA";
e[0].Address = "AAAA";
e[0].Phone = "1204675";
e[0].Country = "US";
e[1] = new Employee();
e[1].Name = "AAAA";
e[1].Company = "AAAA";
e[1].Address = "AAAA";
e[1].Phone = "AAA";
e[1].Country = "AAA";
JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();

return js.Serialize(e);
}

}
[/code]

Replies

  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin
    Replace: `"success": fnCallback,` with:

    [code]
    "success": function ( json ) {
    var d = $.parseJSON( json.d );
    fnCallback( d );
    }
    [/code]

    I've never understood why .NET wants to wrap perfectly good JSON in string, but it does...

    Allan
  • cmanvacmanva Posts: 37Questions: 0Answers: 0
    edited April 2014
    I updated the javascript, but seems to error out when it hits "fnCallback( d );".
    Any other suggestions?


    [code]
    var oTable = $('#auditList').dataTable({
    "bServerSide": true,
    "sAjaxSource": "Services/HCTCService.asmx/TestJSON",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "data": "{}",
    "url": sSource,
    "contentType": "application/json; charset=utf-8",
    "success": function ( json ) {
    var d = $.parseJSON( json.d );
    fnCallback( d );
    },
    "error": handleAjaxException // this sets up jQuery to give me errors
    } );

    },
    "oLanguage": { "sZeroRecords": "No audits available", "sEmptyTable": "No audits available" },
    "bProcessing": true,
    "bDeferRender": true,
    "iDisplayLength": 5,
    "bLengthChange": false,
    "bDestroy": true,
    "bFilter": false,
    "aoColumns": [
    { "sName": "Name" },
    { "sName": "Company" },
    { "sName": "Address"},
    { "sName": "Phone" },
    { "sName": "Country"}
    ],
    "sPaginationType": "full_numbers"
    });
    }
    [/code]
  • cmanvacmanva Posts: 37Questions: 0Answers: 0
    edited April 2014
    Also tried:

    [code]
    f = JSON.stringify(d);
    fnCallback(f);
    [/code]

    I wrote f to a div:

    [code]
    [
    {
    "Name": "AAA",
    "Company": "AAAA",
    "Address": "AAAA",
    "Phone": "1204675",
    "Country": "US"
    },
    {
    "Name": "AAAA",
    "Company": "AAAA",
    "Address": "AAAA",
    "Phone": "AAA",
    "Country": "AAA"
    }
    ]
    [/code]


    The json is formatted correctly.

    Is my html table incorrect?:

    [code]



    Name
    Company
    Address
    Phone
    Country




    [/code]
  • cmanvacmanva Posts: 37Questions: 0Answers: 0
    edited April 2014
    Used a try and catch, I got "unable to get value of the property length : object is null or undefined"

    tried this:

    [code]
    "success": function (json) {
    var d = $.parseJSON(json.d);
    // f = JSON.stringify(d);
    try {
    fnCallback(d);
    }
    catch (error)
    {
    alert(error.message)
    }
    //fnCallback(f);
    },
    [/code]
  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin
    > "bServerSide": true

    I don't see the server-side processing required variables anywhere in your JSON return? See: http://datatables.net/usage/server-side .

    Allan
  • cmanvacmanva Posts: 37Questions: 0Answers: 0
    You are correct Allan, the format I had did not have all the variables.
    I had to change my approach and decided to use WCF instead of ASMX.

    Here is a great article that helped get on track and includes code samples:
    http://www.rantdriven.com/post/Using-Datatablesnet-JQuery-Plug-in-with-WCF-Services.aspx
  • cmanvacmanva Posts: 37Questions: 0Answers: 0
    Btw, Allen thank you for creating a great control. I have used it since 2011and I learned so much about Jquery/AJAX/JSON/REST/API just using it!
This discussion has been closed.