2016-11-29 131 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); 
    }); 

现在,如果您打印修改的响应,你可以看到数据属性改为无论我们正在返回。

然后我们可以执行任何我们需要的数据操作。

相关问题