angularjs - JSON 中位置 0 处出现意外标记 T

标签 angularjs json

当我单击提交按钮时,尽管我的数据已按照我的意愿保存到后端,但我在控制台中收到此错误。

SyntaxError: Unexpected token T in JSON at position 0
    at JSON.parse (<anonymous>)
    at dc (angular.min.js:91)
    at angular.min.js:92
    at q (angular.min.js:7)
    at gd (angular.min.js:92)
    at f (angular.min.js:94)
    at angular.min.js:131
    at m.$digest (angular.min.js:142)
    at m.$apply (angular.min.js:146)
    at l (angular.min.js:97)

这是我的 Angular 前端代码

  $scope.nextStep = function() {
        if ($scope.selection === 'Information'){
            $scope.branch.organisation = $scope.branch.organisation.id;
            $scope.fact.incrementStep($scope);
        }
        else if ($scope.selection === 'Validation'){
            var authdata = base64.encode($rootScope.globals.currentUser.user.phone + ':' + $scope.password.password);
            if (authdata === $rootScope.globals.currentUser.authdata){
                $scope.passwordMatch = true;

                var branchArr = [];
                var dynamicBranches = $scope.dynamicBranches;

                for (var i = 0; i < dynamicBranches.length; i++) {
                  branchArr.push(dynamicBranches[i].name);
                }

                var params = [{
                    "region" : $scope.branch.region,
                    "branches" : branchArr
                }];

                Restangular.one('organisation', $scope.branch.organisation).all('add_region_and_branch_data').post(params).then(function(response) {
                     $scope.createdBranch = response;
                     $scope.fact.incrementStep($scope);
                 }, function(error){
                     ///console.log('Error with status', error.statusText, 'code', error.status);
                     //SweetAlert.swal('Error', 'The agent couldn\'t be created. \n' + error.data.error, 'error');
                     console.log(error);
                 });


            }else{
                $scope.passwordMatch = false;
            }
        }
    };

同样,我的数据已保存到 api,但我收到此错误。我该如何解决这个问题?

最佳答案

检查您的 HTTP 响应正文。 AngularJS 得到一些它无法解析的东西,比如 JSON。是否可能发生任何警告或错误并添加到您的 API 响应中?我认为问题不在您的 nextStep 函数中。

JSON 中位置 0 处的意外标记 T 问题可能会发生,例如,使用以下 HTTP 响应:

Too many params warning{"here": "is valid JSON"}

或者只是警告

Too many params warning

关于angularjs - JSON 中位置 0 处出现意外标记 T,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47318813/

相关文章:

angularjs - Angular $location.path() 未获取基本标签路径

javascript - Angular 错误 : $http. 帖子不是函数

AngularJS - 有条件地应用属性?

c# - 如何从 JArray 中选择元素的值

javascript - 使用 AngularJS 从两个不同的 $scopes 添加数字

javascript - Angular 中的 JWT 表现不佳——出现无效 header 错误

json - Jmeter JSON 路径断言

php - 如何从新的 twitter api 1.1 获取推文?

javascript - 将 JSON 对象推送到数组

javascript - 使用 AngularJS 在 Google map 中实现标记