AngularJS SyntaxError:意外的令牌{

wob*_*ano 0 javascript php angularjs

我有一个有效的代码,但当我检查我的控制台时,我得到了SyntaxError: Unexpected token {这里的截图:

在此输入图像描述

line 58studentController.js在这里查看它时console.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);
          });
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么吗?谢谢.

更新:

管理通过纠正一些变量来修复它.在我的PHP文件中:

$request = Slim\Slim::getInstance()->request();
$data = json_decode($request->getBody());
echo json_encode($data); 
Run Code Online (Sandbox Code Playgroud)

它应该是

echo json_encode($request); 
Run Code Online (Sandbox Code Playgroud)

小智 5

您的json响应JSON格式不正确.修复它.