簡體   English   中英

AngularJS SyntaxError:意外令牌{

[英]AngularJS SyntaxError: Unexpected token {

我有一個有效的代碼,但是當我檢查控制台時,出現SyntaxError: Unexpected token { ,這是屏幕截圖:

在此處輸入圖片說明

當我在這里檢查studentController.js line 58 studentController.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);
          });
    }
}

我在這里想念什么嗎? 謝謝。

更新:

設法通過更正一些變量來修復它。 在我的php文件中:

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

它應該是

echo json_encode($request); 

您的json響應不是有效的JSON格式。 解決這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM