1

I am trying start with promises win angularJS.

But, I am getting an error when AngularJS is parsing the result from the backend.

What is wrong here?

Here is my HTML:

<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 my JSON data returning from the backend:

{"draw":,"recordsTotal":5303,"recordsFiltered":5303,
"data":[{"DT_RowId":"4367","pront":"4367","nome":"XXXXXXXXX","endereco":"RUA TEODORO DA SILVA,294\/314","bairro":"VILA ISABEL","cidade":"RIO DE JANEIRO","estado":"RJ","telefone":"2567*0440","cpf":"","email":""},
{"DT_RowId":"21","pront":"21","nome":"YYYYYYYYY","endereco":"R ARAGUAIA","bairro":"PARQUE CHUNO","cidade":"DUQUE DE CAXIAS","estado":"RJ","telefone":"35637685","cpf":"02570293709","email":"[email protected]"},
{"DT_RowId":"23","pront":"23","nome":"ZZZZZZZZZZ","endereco":"rua 18 de outubro 241 101","bairro":"tijuca","cidade":"RIO DE JANEIRO","estado":"RJ","telefone":"","cpf":"","email":""},
{"DT_RowId":"24","pront":"24","nome":"AAAAAAAAAAA","endereco":"RUA MARIZ E BARROS 470 APTO 610","bairro":"TIJUCA","cidade":"RIO DE JANEIRO","estado":"RJ","telefone":"22646701","cpf":"53551192715","email":""},
{"DT_RowId":"27","pront":"27","nome":"AAAAAAAA GON\u00C7ALVES","endereco":"rua an\u00E1polis 251","bairro":"nova igua\u00E7u","cidade":"RIO DE JANEIRO","estado":"RJ","telefone":"3101-9648","cpf":"","email":""},
{"DT_RowId":"28","pront":"28","nome":"ASKLJALDJSLKADJ","endereco":"lucio de mendon\u00E7a 24 apt 501","bairro":"maracana","cidade":"RIO DE JANEIRO","estado":"RJ","telefone":"2568-9519","cpf":"04301072772","email":""},
{"DT_RowId":"30","pront":"30","nome":"SADFSADFASDFSD","endereco":"RUA GRAVATAI N 61 APTO 302","bairro":"ROCHA MIRANDA","cidade":"RIO DE JANEIRO","estado":"RJ","telefone":"32787747","cpf":"","email":""},
{"DT_RowId":"29","pront":"29","nome":"ANASADFSA DOS SANTOS","endereco":"saboia lima 12 apt 04","bairro":"tijuca","cidade":"RIO DE JANEIRO","estado":"RJ","telefone":"2204-1498","cpf":"48080152268","email":""},
{"DT_RowId":"31","pront":"31","nome":"JOAO SDAFSA SOUZA","endereco":"av dom helder camara 312 bl 05 apt 102","bairro":"benfica","cidade":"RIO DE JANEIRO","estado":"RJ","telefone":"","cpf":"075422437-64","email":""},
{"DT_RowId":"33","pront":"33","nome":"SKDJFSDAJFLASD","endereco":"fabio da luz 275 bl 04 apt 504","bairro":"meier","cidade":"RIO DE JANEIRO","estado":"RJ","telefone":"3979-0859","cpf":"","email":""}]}

I am getting the next error in my JSON return:

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)

JS CODE:

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();
              }
            })();
3
  • 1
    There is no value returned against "draw". That is why the error is occurring. Add null to response and it will be fixed. Commented Feb 8, 2017 at 13:38
  • 1
    your JSON is wrong: "draw":, Commented Feb 8, 2017 at 13:38
  • Thanks to all. I am a stupid. I didn´t see it. Commented Feb 8, 2017 at 13:49

3 Answers 3

2

It appears that the JSON is invalid. Specifically, on the first line:

{"draw":,"recordsTotal":5303, .....

The "draw" member does not have a value. You can test your JSON using a linter like jsonlint

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I just put the value into draw in the backend. It solves the error. I stiil have a problem. My html view is not showing the data. Can you tell me what is wrong in the html?
Hey @LuizAlves , glad it help you out. I'm not familiar with the ngTable directive, hence I'm not sure what could be wrong with that. You may want to ask a different question specific to the ngTable issue.
0

The JSON response is invalid so the parsing fails. The key "draw" does not have a value, thus the invalid character at position 8 error.

You can read more about valid JSON objects here: http://json-schema.org

Some examples here: http://json-schema.org/examples.html

Comments

0

Using a JSON validator it shows that there is an error in the JSON format you are getting back

Error: Parse error on line 1:

"draw": ,"recordsTotal": ------^

Expecting 'EOF', '}', ',', ']', got ':'

You need to look at the construction of the object from the backend

1 Comment

In response to your html not showing your data, make sure you are setting the scope to the returned data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.