My problem is, it doesn’t display table from c# after I input text and click search button. I have tried many ways, but it can’t work. Below is my code.
Below is my javascript:
url: "/Testing/TraceReport/traceReport.aspx/app",
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var EquipmentList = xml.find("Table1");
var content = "";
if (EquipmentList.length > 0) {
$(EquipmentList).each(function () {
content += "<tr align='center'>";
if ($(this).find('Column1').text() == "") { content += "<td><b><font color='red'>UNKNOWN</font></b></td>"; }
else { content += "<td><b>" + $(this).find('Column1').text() + "</b></td>"; }
if ($(this).find('Column2').text() == "") { content += "<td><b><font color='red'>UNKNOWN</font></b></td>"; }
else { content += "<td><b>" + $(this).find('Column2').text() + "</b></td>"; }
if ($(this).find('Column3').text() == "") { content += "<td><b><font color='red'>UNKNOWN</font></b></td>"; }
else { content += "<td><b>" + $(this).find('Column3').text() + "</b></td>"; }
if ($(this).find('Column4').text() == "") { content += "<td><b><font color='red'>UNKNOWN</font></b></td>"; }
else { content += "<td><b>" + $(this).find('Column4').text() + "</b></td>"; }
if ($(this).find('Column5').text() == "") { content += "<td><b><font color='red'>UNKNOWN</font></b></td>"; }
else { content += "<td><b>" + $(this).find('Column5').text() + "</b></td>"; }
if ($(this).find('Column7').text() == "") { content += "<td><b><font color='red'>UNKNOWN</font></b></td>"; }
else { content += "<td><b>" + $(this).find('Column7').text() + "</b></td>"; }
content += "</tr>";
});
}
Below is my c#:
DataTable table = new DataTable();
table = ConvertListToDataTable(list);
DataSet ds = new DataSet();
ds.Tables.Add(table);
return ds.GetXml();
2
Answers
The
dataType
property tells the server what type of data to return.You’re telling the server you want to receive
json
. Yet, it looks like the server is sendingxml
. Update thedataType
in theajax
method:Well as far as I understand, You are returning the DataTable object itself from your
WebMethod
and you want to read all the returnedrows
from thecolumn
.Here is my suggestion to do it, Make a model and copy your values from the
DataTable
to a C# class object Generic List. Below is the idea:Then in your
WebMethod
:Then in your ajax call:
This is a rough idea how to achieve the task you are doing. Hope it helps.
PS: I didn’t used any compiler so the code is not tested, it may need some love.