Angularjs SyntaxError:JSON.parse(<;anonymous>;)位置0处的JSON中出现意外标记R
无法进入success()函数,而是获取语法错误“JSON.parse()位置0处JSON中的意外令牌R”。 但是所有的后台数据库操作都在正常进行 注意:我没有从AJAX调用返回任何JSON数据Angularjs SyntaxError:JSON.parse(<;anonymous>;)位置0处的JSON中出现意外标记R,angularjs,json,Angularjs,Json,无法进入success()函数,而是获取语法错误“JSON.parse()位置0处JSON中的意外令牌R”。 但是所有的后台数据库操作都在正常进行 注意:我没有从AJAX调用返回任何JSON数据 <html ng-app="PlaylistApp"> <head> <meta charset="utf-8"> <title>Angular.js Example</title> <link rel="stylesheet" type
<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>
Angular.js示例
var playliapp=angular.module('playliapp',[]);
playliapp.controller('playlictrl',函数($scope,$http)
{
$http({
方法:“GET”,
网址:'http://localhost:8080/SignageSoC/api/playlist/all'
}).然后(功能成功(响应){
$scope.playlists=response.data;
});
$scope.removePlaylist=函数(索引,播放ID)
{
var i=指数;
警报(一);
警报(播放ID);
$http({
方法:“删除”,
网址:'http://localhost:8080/SignageSoC/api/playlist/“+播放ID
}).然后(函数成功(){
警报(一);
$scope.playlists.splice(i,1);
});
}
});
播放ID
名称
总持续时间
连续播放
开始时间
结束时间
使现代化
去除
{{x.playlaid}
{{x.name}
{{x.总持续时间}
{{x.play_continuous}}
{{x.start_time}
{{x.end_time}
使现代化
删去
事实证明,我们有办法避免这种异常。
来自angular ajax调用的任何响应都需要一个承诺(将在内部解析为一个对象),JSON.parse将在该响应对象上自动调用
如果我们没有返回任何JSON对象(在我的例子中为true),那么angular会在JSON.parse()异常的0位置抛出一个JSON中的意外标记(任何字母)
要使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);
});
现在,如果您打印修改后的响应,您可以看到数据属性更改为我们返回的内容
然后我们可以对该数据执行所需的任何操作。“但我不想解析代码中的任何内容”-请:默认转换“…响应转换…如果检测到JSON响应,请使用JSON解析器对其进行反序列化。@Quentin,我看了一眼文档,但在这里我只是返回一个字符串,表示特定内容已被删除。我不想对这个响应做任何事情,我只想在它成功的时候做一个拼接操作。Angular认为你是在用JSON响应并抛出一个错误,因为它不是有效的JSON。@Quentin有没有解决方法来避免这个问题,或者必须返回一个JSON对象?我假设解决方案是有效的“在响应中发送正确的内容类型”