How to load data JSON to DataTable?
How to load data JSON to DataTable?
headshot9x
Posts: 59Questions: 16Answers: 1
Hello guys. I use DataTable load data from webservice. I debug and get result at http://debug.datatables.net/ixacav (please see it).
And this is WebMethod
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string ListLocation()
{
string json = "";
using (QLTDEntities db = new QLTDEntities())
{
var list = db.LOCATIONs.Where(e => e.FLAG == true).ToList().Select(e => new LOCATION()
{
LOCATION_ID = e.LOCATION_ID,
AREA_ID = e.AREA_ID,
LOCATION_NAME = e.LOCATION_NAME,
LOCATION_DES = e.LOCATION_DES,
EDIT_DATE=e.EDIT_DATE,
EDIT_BY=e.EDIT_BY,
FLAG=e.FLAG
}).ToList();
foreach (var item in list)
{
item.TOPICs.ToList().ForEach(i => i.LOCATION = null);
item.USERS_PROFILE.ToList().ForEach(i => i.LOCATION = null);
}
return json = JsonConvert.SerializeObject(list);
}
}
Page.aspx not load data . Please give me your advice. Thank you .(as allan)
This question has accepted answers - jump to:
This discussion has been closed.
Answers
Your Ajax response is not valid JSON - that needs to be corrected (how exactly, I don't know. I would suggest asking in a C# forum if you don't know yourself).
Also remove
serverSide: true
. You have not implemented server-side processing.Allan
I have remove follow your advice
My Ajax Json is right , I test webservice and get result
Tell me about problem in here ? thank you so much.
That is not valid JSON. You need to lose the
<string></string>
tags.You can use JSONLint to test your JSON. As tangerine says, it is not valid.
Allan
Sorry I did not answer. I confirm that this is the JSON string returned when running Webservice, because when I run Webservice returns the result as such. I've searched and tested a lot through google, JSON string using Webservice to run and the results are the same.
I just do not understand when running Datatables the back of the results is
(I've used DevTool of GoogleChorme to see: Network> XHR and see my JSON string is added to the
Please give me some advice. Thank you.
It might be that you need to use
contentType
in the Ajax request to force the .NET web service to return JSON rather than XML.As we noted above, the current return is not valid JSON. You need to modify something (be it in the request or the server-side) to have it return valid JSON.
Allan
Dear allan. Thank for your reply. I will show all code in my page which is try . I think problem at Datatable. I apologize because I have to write so much so, I thought to write specific to you and other people can understand the problem I was suffering.If I write a detailed and specific so you can understand all the issues I was faulty.
Ok, First in my Webservice , I have a function ListLocation as return JSON
I run it and get Result as JSON
In next step , I use with test.aspx , I use DdaTable is lasted.
Ok,. this time i get info from DevTool Chrome
It return Json as follow in Respone
And it is get error at
Well , can you give me some advice . Thank you so much.
Your "JSON" is being returned with a single parameter (
d
) which is a string. You need to JSON decode the rest. You could useajax.dataSrc
for that:Allan
Dear allan.
It's good to know my JSON string is always right. The error here is how I declare to the DataTable ajax. I am very sorry about this issue, I will refer to many articles about issues . Again thank you.