2017-02-28 105 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

相關問題