有人可以帮我找出我的语法错误在哪里?意外的令牌Angular JS

时间:2015-12-23 06:51:54

标签: angularjs rest

Rest服务器返回String。我试图在REST中获得授权并回归角度。但我卡在这里,无法找到我的语法错误。非常感谢你!

        app.controller('lController', function($scope, $http)
        {
           $scope.login = function()
           {
            console.log("Login in");
        var endPoint = "http://localhost:8080/RestServer/main/just/Authorize";
        var jsonData = {
            username: $scope.username,
            password: $scope.password
        };
        var jsonString = JSON.stringify(jsonData);

        $http.post(endPoint, jsonString,{headers: {'Content-Type': 'application/json'}}).success(function (data,status, headers ){
            var d = JSON.parse(data);
            if(d.access == "ok")
            {
                Console.log(d);
                sh = d.id;
            }

        });

    }
});

2 个答案:

答案 0 :(得分:0)

不确定您的语法错误在哪里,但对于初学者而言,您并不需要全部使用JSON。 试试这个

   app.controller('lController', function($scope, $http) {
       $scope.login = function() {
           console.log("Login in");
           var endPoint = "http://localhost:8080/RestServer/main/just/Authorize";
           var userData = {
               username: $scope.username,
               password: $scope.password
           };

           $http.post(endPoint, userData).success(function(data, status, headers) {
               if (data.access && data.access == "ok") { //<-- checking for property existince like this is probably not the way to go, but it'll do here
                   Console.log(d);
                   sh = d.id;
               }

           });
       }
    });

注意:确保您的服务器将内容返回为application/json

答案 1 :(得分:0)

  1. Console用大写字母代替console
  2. 是否定义了变量sh