I can't figure out how to get data from the JSON object. I always get an error.
I can't figure out how to get data from the JSON object. I always get an error.
scottdk
Posts: 1Questions: 1Answers: 0
At the very bottom of this HTML is the contents of the JSON object. The JSON object is contained within the data parameter of the formatData function. What am I doing wrong here?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test calling web service via jquery ajax</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script>
function callSvc() {
$.ajax({
type: "POST",
url: "http://mywebservice.asmx/getCondition",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: formatData,
error: displayError
});
function formatData(data, status) {
$('#tblData').DataTable({
"ajax": "data.json",
"columns": [
{ "data": "Route" },
{ "data": "Date" },
{ "data": "County" },
{ "data": "Condition" }
]
});
}
function displayError() {
$("#msg").text("There was a problem displaying the data. Please try again later.")
}
}
</script>
</head>
<body>
Test calling web service via jquery ajax.
<div id="msg"></div>
<table id="tblData" width="100%" cellspacing="0">
<thead>
<tr>
<td>Route</td>
<td>Date</td>
<td>County</td>
<td>Condition</td>
</tr>
</thead>
<tfoot>
<tr>
<td>Route</td>
<td>Date</td>
<td>County</td>
<td>Condition</td>
</tr>
</tfoot>
</table>
<script>
$(document).ready(function () {
callSvc();
});
</script>
<!--
[[
{"Type":"I -","Route":20,"additional":null,"pkid":83,"dtmdate":"\/Date(1424963580000)\/","strStatus":7,"County_Name":"Richland","PKConditionCode":7,"strConditionDescription":"No condition reported"},
{"Type":"I -","Route":26,"additional":null,"pkid":84,"dtmdate":"\/Date(1424963580000)\/","strStatus":7,"County_Name":"Richland","PKConditionCode":7,"strConditionDescription":"No condition reported"},
{"Type":"I -","Route":77,"additional":null,"pkid":85,"dtmdate":"\/Date(1424963580000)\/","strStatus":7,"County_Name":"Richland","PKConditionCode":7,"strConditionDescription":"No condition reported"},
]]
-->
</body>
</html>
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This discussion has been closed.
Answers
That will cause DataTables to send an Ajax request to
data.json
on the server.You probably want to use
data
rather thanajax
, since you have already made the Ajax call to get the data. Something like:Allan