Asmx web service (framework 3.5) not returning proper format JSON?
Asmx web service (framework 3.5) not returning proper format JSON?
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]
[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]
This discussion has been closed.
Replies
[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
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]
[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]
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]
I don't see the server-side processing required variables anywhere in your JSON return? See: http://datatables.net/usage/server-side .
Allan
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