我一直卡在這個錯誤上。我也檢查了其他類似的問題。我有一個簡單的angularjs/spring應用程序。該請求提供了200,但我在響應的開發人員工具/螢火蟲中收到JSON分析異常。服務器的響應是一個簡單的字符串,我可以看到。我懷疑問題是角度是解析它爲JSON。請幫我解決一下這個。錯誤:JSON.parse:來自Json的JSON數據的第1行第1列的意外字符
角控制器:
var loginApp = angular.module('loginApp',[]);
loginApp.controller('loginController', ['$scope','$http',function($scope,$http) {
$scope.login = function(isValid) {
if(isValid){
var formData= {"username":$scope.username,"password":$scope.password};
$http.post('/suite/rest/dologin',formData).success(function(response){
console.log(response);
alert(response);
});
}
};
}]);
春控制器:
@RequestMapping(value = "/dologin", method = RequestMethod.POST)
public @ResponseBody String doLogin(@RequestBody final User user) {
String validateUser = "admin";
return validateUser;
}
錯誤消息:
Error: JSON.parse: unexpected character at line 1 column 1 of the JSON data
[email protected]://localhost:8080/suite/extResources/Angular/angular.js:1352:9
[email protected]://localhost:8080/suite/extResources/Angular/angular.js:10455:1
transformData/<@http://localhost:8080/suite/extResources/Angular/angular.js:10546:12
[email protected]://localhost:8080/suite/extResources/Angular/angular.js:322:11
[email protected]://localhost:8080/suite/extResources/Angular/angular.js:10545:3
[email protected]://localhost:8080/suite/extResources/Angular/angular.js:11343:21
[email protected]://localhost:8080/suite/extResources/Angular/angular.js:16104:28
scheduleProcessQueue/<@http://localhost:8080/suite/extResources/Angular/angular.js:16120:27
$RootScopeProvider/this.$get</Scope.prototype.$e[email protected]://localhost:8080/suite/extResources/Angular/angular.js:17378:16
$RootScopeProvider/this.$get</[email protected]://localhost:8080/suite/extResources/Angular/angular.js:17191:15
$RootScopeProvider/this.$get</[email protected]://localhost:8080/suite/extResources/Angular/angular.js:17486:13
[email protected]://localhost:8080/suite/extResources/Angular/angular.js:11637:36
[email protected]://localhost:8080/suite/extResources/Angular/angular.js:11843:7
[email protected]://localhost:8080/suite/extResources/Angular/angular.js:11776:1
用戶等級:
public class User implements Serializable {
@JsonProperty
private String username;
@JsonProperty
private String password;
public User(){
}
public User(String username, String password){
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
那「簡單的字符串」究竟是什麼樣子?哦,等一下,我明白了。那麼'admin'文本肯定是無效的JSON。一個普通的字符串值必須用雙引號括起來。 – Pointy
請同時發佈JSON數據和用戶類 –
validateUser =「admin」;是字符串,而不是json對象。 –