语法错误:在JSON的位置0处意外出现标记R,无法解析。 (匿名函数)中的JSON.parse

3
无法进入success()函数,而是出现语法错误:“在JSON的位置0处意外的R令牌”。但所有后台数据库操作都按照预期进行。
注意:我没有从AJAX调用中返回任何JSON数据。
<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>

但是我并不试图在代码中解析任何东西。请阅读文档:默认转换`...响应转换...如果检测到JSON响应,则使用JSON解析器对其进行反序列化。 - Quentin
@Quentin,我瞥了一眼文档,但现在我只是返回一个字符串,说特定内容已被删除。而且我没有试图对该响应执行任何操作,我只想在成功时执行拼接操作。 - Sarath Raja
我认为解决方案是“在响应中发送正确的内容类型”。 - Quentin
我说的是“在响应中”。不是请求,而是响应。 - Quentin
你能告诉我如何使它返回一个字符串吗?因为 Angular.js 处理的是 promises,所以我无法做到你说的那样? - Sarath Raja
显示剩余5条评论
2个回答

4
原来我们有一种方法可以避免出现这个异常。 任何来自Angular的AJAX调用都需要一个承诺对象(该对象将在内部被解析为一个对象),并且JSON.parse将自动在响应对象上调用。
如果我们没有返回任何JSON对象(在我的情况下是真的),那么Angular会抛出一个“在JSON的位置0处意外的标记(任何字母)”的异常。
为了使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);
    });

现在,如果您打印修改后的响应,您可以看到数据属性已被更改为我们返回的内容。
然后,我们可以对该数据执行任何需要的操作。

1
这通常发生在应用程序期望API调用返回JSON响应时。首先确保你的API开始返回JSON响应。(在我的情况下,我得到了一个字符串类型的返回值,并将其格式化为JSON可以解决问题。)
另外,还有可能是通常负责生成和返回字符串化JSON的服务器端功能失败了。有许多原因导致这种情况,但你应该从检查服务器端代码并确保它正确开始。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接