2017-02-28 103 views
0

我在控制台中出现此错误,但单击提交按钮时虽然我的数据正按照我的意愿保存到后端。在位置0的JSON中出现意外的令牌T

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) 

这里是角

$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,但我得到这个错误我的前端代码。我怎样才能解决这个问题?

+1

您需要调试。你的服务返回什么?这可能是无效的JSON,因为他们有一些错误。 –

回答

1

检查您的HTTP响应正文。 AngularJS得到了像JSON那样无法解析的东西。可能是任何警告或错误发生并添加到您的API响应?我的问题不在你的nextStep函数中。

Unexpected token T in JSON at position 0问题可能发生,例如,与此HTTP的响应:

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

或者只是警告

Too many params warning 
+0

我需要使用organisation_id以这种格式[{“region”:“matlab”,“branches”:[“B”,“C”]}]发送数据。 –

+0

@SalmanMahmud不幸的是,没有HTTP请求和响应转储,我无法帮到你。您能否通过Chrome DevTools Network标签显示此请求的数据? – Antonio

相关问题