Help getting data from server with option ajax (DataTavle v1.10.9)
Help getting data from server with option ajax (DataTavle v1.10.9)
Hi,
I'm really sorry for the newbie question, but I'm just starting with jquery and ajax and all.
Currently I'm trying to create a DataTable that gets data from a WebService (asmx because its the currently technology i know).
From what i can see from this page () i should just add the ajax option and give it my webservice method url, unfortunatly this is not working for me. Whatching the debug i get no error, so I must defenitly be missing something but i don't know what :( I know the call is not being made because I have a breakpoint in my method and its not getting there.
So far the only way I've managed to make DataTable work is by making a similiar example i found that makes an ajax call and then sets option aaData
with the returned result. Although it works, I would like to remove the extra function and make something a bit more generic (talking about the object it receives).
NOTE: the server-side coded should be irrelevant for my problem since its not being reached at all.
Here follows my code:
HTMl + JS (in a aspx page)
<table id="sampleTable2" class="display">
<thead>
<tr>
<th>Structure_code
</th>
<th>Descritpion
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
...
$(document).ready(function () {
$('sampleTable2').DataTable({
"processing" : true,
"serverSide" : true,
"ajax" : "/WebService1.asmx/GetItems2"
});
});
Server-Side (C#)
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Test> GetItems2()
{
System.Data.OleDb.OleDbCommand cmm;
System.Data.OleDb.OleDbConnection conn;
System.Data.DataSet ds = new System.Data.DataSet();
using (cmm = new System.Data.OleDb.OleDbCommand())
{
cmm.CommandType = System.Data.CommandType.Text;
cmm.CommandText = "Select structure_code, description from structure where structure_name = '$Plan' and rownum <= 100";
using (conn = new System.Data.OleDb.OleDbConnection("XXXXXXXXXXXXXXX"))
{
conn.Open();
cmm.Connection = conn;
try
{
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(cmm);
adapter.Fill(ds);
}
catch
{
}
finally
{
conn.Close();
}
}
}
List<Test> x = new List<Test>();
foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
{
x.Add(new Test { structure_code = dr[0].ToString(), description = dr[0].ToString() });
}
return x;
}
public class Test
{
public string structure_code, description;
}
Answers
In the browser debugger, watch the network traffic. I'm guessing that your URL for the ajax call isn't right. You are probably getting a 404 on that.