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?

1
  • there is no need to use JSON.parse inside $http.get success, it will already be a well formed JSON, refer this answer Commented May 7, 2016 at 20:05

1 Answer 1

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

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

1 Comment

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"

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.