2016-11-29 40 views
0

無法輸入success()函數,而是在位於JSON.parse()位置0的JSON中出現'Unexpected token R'語法錯誤。 但是所有的後臺數據庫操作都是按照他們應該進行的。SyntaxError:JSON中位置0處JSON.parse處的意外標記R(<anonymous>)

注:我不是從AJAX調用

<html ng-app="PlaylistApp"> 
<head> 
<meta charset="utf-8"> 
<title>Angular.js Example</title> 
<link rel="stylesheet" type="text/css" href="style.css"> 
<script src="lib/angular.min.js"></script> 
<script> 
     var playlistApp = angular.module('PlaylistApp', []); 
     playlistApp.controller('PlaylistCtrl', function ($scope, $http) 
     { 
      $http({ 
        method : 'GET', 
        url : 'http://localhost:8080/SignageSoC/api/playlist/all' 
        }).then(function success(response) { 
        $scope.playlists = response.data; 
       }); 
      $scope.removePlaylist = function(index, playlistId) 
      {  
      var i = index; 
      alert(i); 
      alert(playlistId); 
      $http({ 
        method : 'DELETE', 
        url : 'http://localhost:8080/SignageSoC/api/playlist/'+ playlistId 
       }).then(function success() { 
        alert(i); 
        $scope.playlists.splice(i, 1); 
       }); 
      } 
    }); 
</script> 
</head> 
<body ng-controller="PlaylistCtrl"> 
    <div> 
     <br> 
     <br> 
     <br> 
     <table class="center"> 
      <tr> 
       <th>PlaylistId</th> 
       <th>Name</th> 
       <th>Total_duration</th> 
       <th>Play_continuous</th> 
       <th>Start_time</th> 
       <th>End_time</th> 
       <th>Update</th> 
       <th>Remove</th> 
      </tr> 
      <tr data-ng-repeat="(index,x) in playlists"> 
       <td>{{x.playlistId}}</td> 
       <td>{{x.name}}</td> 
       <td>{{x.total_duration}}</td> 
       <td>{{x.play_continuous}}</td> 
       <td>{{x.start_time}}</td> 
       <td>{{x.end_time}}</td> 
       <td><button data-ng-click="updatePlaylist(index)">Update</button></td> 
       <td><button data-ng-click="removePlaylist(index, x.playlistId)">Delete</button></td> 
      </tr> 
     </table> 
    </div> 
</body> 
</html> 
+0

「但我不打算解析代碼中的任何東西」 - 請[閱讀文檔](https://docs.angularjs.org/api/ng/service/$http):** Default Transformations ...響應轉換...如果檢測到JSON響應,請使用JSON解析器對其進行反序列化。** – Quentin

+0

@Quentin,我看了一下文檔,但在這裏我只是返回一個字符串,表示特定的字符已被刪除。而且我並沒有試圖對此做出任何反應,我只是想在成功時進行拼接操作。 –

+0

而Angular認爲你用JSON響應並拋出一個錯誤,因爲它不是有效的JSON。 – Quentin

回答

1

返回任何JSON數據原來有辦法,我們如何才能避免這種例外。 任何來自角度ajax調用的響應都需要承諾(將在內部解析爲對象),並且JSON.parse將自動在該響應對象上調用。

如果我們沒有返回任何JSON對象(在我的情況下爲true),則角度將在JSON中的位置0在JSON.parse()異常處拋出一個意外標記(任何字母)。

要使ajax調用接收除對象以外的任何數據,我們必須使用內置配置,稱爲transformResponse屬性,並使解析器知道我們沒有使用任何JSON數據。

要做到這一點我用下面的方式

$http({ 
     method : 'DELETE', 
     url : 'http://localhost:8080/SignageSoC/api/playlist/'+ playlistId, 
     transformResponse: function(response) 
          { 
            //alert("Success() starts here"); 
            //alert(response);   
            return response;      
          } 
    }).then(function success(modResponse) { 
     alert(JSON.stringify(modResponse)); 
     alert(JSON.stringify(modResponse.data)); 
     //$scope.playlists.splice(index, 1); 
    }); 

現在,如果您打印修改的響應,你可以看到數據屬性改爲無論我們正在返回。

然後我們可以執行任何我們需要的數據操作。

相關問題