0

I have a code that works but when I check my console I'm getting SyntaxError: Unexpected token { and here is the screenshot:

enter image description here

And when I check line 58 of my studentController.js here it is console.log(error);. The complete code is:

angular
    .module('studentInfoApp')
    .factory('Student', Student)
    .controller('StudentsController', StudentsController)
    .config(config);

function config($routeProvider) {
    $routeProvider
    .when('/', {
        controller: 'StudentsController',
        templateUrl: 'app/views/student-list.html'
    })
}

function Student($resource) {
    return $resource('/students/:id');
}

function StudentsController(Student, $scope, $http) {

    $scope.inputForm = {};
    $scope.students = null;

    function initStudents() {
        Student.query(function(data, headers) {
            $scope.students = data;
            $scope.studentsPanel = true;
            console.log(data);
        }, function (error) {
            console.log(error);
        });
    }

    initStudents();

    $scope.addStudentForm = function() {
        $scope.loading = true;
    }

    $scope.cancelAddStudent = function() {
        $scope.loading = false;
    }

    $scope.addStudent = function() {
        $scope.studentsPanel = false;
        var data = {
            fname: $scope.inputForm.fname,
            lname: $scope.inputForm.lname,
            age: $scope.inputForm.age,
            email: $scope.inputForm.email
        }

        Student.save(data)
        .$promise.then(function successCallback(response) {
            initStudents();
            console.log(response); //line 58
          }, function errorCallback(error) {
            console.log(error);
          });
    }
}

Am I missing something here? Thank you.

UPDATE:

Managed to fix it by correcting some variable. In my php file:

$request = Slim\Slim::getInstance()->request();
$data = json_decode($request->getBody());
echo json_encode($data); 

It should be

echo json_encode($request); 
5
  • Do you need a semi colon after var data = { ... }? Commented Oct 1, 2015 at 11:08
  • @JackalopeZero With or without, still the same.. Commented Oct 1, 2015 at 11:09
  • Can you make jsfiddle? Commented Oct 1, 2015 at 11:11
  • 1
    There seems to be some strange JSON coming in from your request. Can you check the network inspector in your developer tools and post the response of the HTTP request? Commented Oct 1, 2015 at 11:12
  • fixed it already guys. I updated my post. Commented Oct 1, 2015 at 11:20

1 Answer 1

5

Your json response is not in valid JSON format. Fix that.

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

2 Comments

I strongly support this answer, this seems to be a problem with the received JSON-data, the JS is correct, even if a little strange. If you may post the raw data you recieve from the $ressource call, that would help.
You guys are right. I updated my post so you can see what I did with your help.

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.