I am trying to work with promises in AngularJS.
However, I encountered an error while parsing the backend response in AngularJS.
What could be the issue here?
This is the HTML code:
<div ng-app="clinang" ng-controller="pacientesCtrl">
<a class='btn btnprimary' href='/getdadospac/?oper=S' >Button</a>
<table ng-table="tableParams" class="table" show-filter="true">
<tr ng-repeat="paciente in $data">
<td title="'Pront'" filter="{ name: 'text'}" sortable="'pront'">
{{paciente.pront}}</td>
<td title="'Nome'" filter="{ age: 'number'}" sortable="'nome'">
{{paciente.nome}}</td>
</tr>
</table>
</div>
Here is the JSON data retrieved from the backend:
{"draw":,"recordsTotal":5303,"recordsFiltered":5303,
"data":[{...}, {...}, ...]}
The error message related to JSON parsing is as follows:
SyntaxError: Unexpected token , in JSON at position 8
at JSON.parse (<anonymous>)
at fromJson (http://127.0.0.1:8888/files/lib/angular/angular.js:1377:14)
at defaultHttpResponseTransform (http://127.0.0.1:8888/files/lib/angular/angular.js:11003:16)
And here is the JS code snippet:
var app = angular.module("clinang", ["ngTable", "ngResource"]);
(function() {
app.controller("pacientesCtrl", pacientesCtrl);
pacientesCtrl.$inject = ["NgTableParams", "$resource"];
function pacientesCtrl(NgTableParams, $resource) {
// tip: to debug, open chrome dev tools and uncomment the following line
debugger;
var Api = $resource("/getdadospac/?oper=S");
this.tableParams = new NgTableParams({}, {
getData: function(params) {
// ajax request to api
return Api.get(params.url())
.$promise
.then(function(rows) {
debugger;
console.log(rows);
params.total(rows.recordsTotal); // recal. page nav controls
return rows.data;
});
}
});
this.tableParams.reload();
}
})();