2015-10-01 127 views
0

我有工作,但是当我检查我的控制台我越来越SyntaxError: Unexpected token {,这里是截图代码:AngularJS语法错误:意外标记{

enter image description here

当我检查我的studentController.jsline 58这里它是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); 
      }); 
    } 
} 

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

UPDATE:

管理通过校正一些变量来解决它。 在我的PHP文件:

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

应该

echo json_encode($request); 
+0

'var data = {...}'后面是否需要分号? – JackalopeZero

+0

@JackalopeZero有或没有,仍然一样.. – FewFlyBy

+0

你可以做jsfiddle吗? –

回答

5

你的JSON响应不是有效JSON格式。修复。

+0

我强烈支持这个答案,这看起来与接收到的JSON数据有关,JS是正确的,即使有点奇怪。如果您可以发布您从$ ressource调用收到的原始数据,这将有所帮助。 – Lacrioque

+0

你们是对的。我更新了我的文章,以便您了解我的帮助。 – FewFlyBy