0

i have a problem when i want send data from Express to Angular I get error:

SynatxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

File: controller.client.js

$scope.getData = function() {
        $http.get('http://localhost:3000/getData').then(function(response) {
            alert('good: ');
            console.log(JSON.parse(response));
        }, function(error) {
            alert('Error: ');
            console.log("error: ");
            console.log(error);
        });
    } 

File: server.controller.js

exports.getData = function(req, res) {
    var data = {
        "name": "kacper",
        "age": 12
    };
    console.log('getData function: ');
    console.log(data);

    res.json(data);
}

File server.routes.js

app.get('/getData', index.getData);

and always in my response is error:

SynatxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

how to fix that?

Kaker
  • 637
  • 1
  • 11
  • 23
  • there is no need to use `JSON.parse` inside `$http.get` success, it will already be a well formed JSON, refer [this answer](http://stackoverflow.com/a/29763622/2435473) – Pankaj Parkar May 07 '16 at 20:05

1 Answers1

0

From your server you are sending your response by res.json(data).

res.json() Sends a JSON response. When angular gets that response it's already a JSON. You don't need to parse it again. Use it as is.

Read more: http://expressjs.com/en/4x/api.html#res.json

Vikram Tiwari
  • 3,615
  • 1
  • 29
  • 39
  • in console i get a this data from sever: "error: home.client.controller.js:69:13 Object { data: null, status: -1, headers: headersGetter/<(), config: Object, statusText: "" }" and still the same error in response: "SynatxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data" – Kaker May 08 '16 at 10:07