AngularJS SyntaxError:意外的令牌{

时间:2015-10-01 11:05:59

标签: javascript php angularjs

我有一个有效的代码,但当我检查我的控制台时,我收到了117258,30759....................................................... 279576,38463....................................................... 99200,11223........................................................ ,这是截图:

enter image description here

当我在SyntaxError: Unexpected token {检查line 58时,studentController.jsconsole.log(error);。完整的代码是:

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);
          });
    }
}

我在这里遗漏了什么吗?谢谢。

更新

通过更正某些变量来管理修复它。 在我的php文件中:

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

应该是

echo json_encode($request); 

1 个答案:

答案 0 :(得分:5)

您的json响应不是有效的JSON格式。解决了这个问题。