Ich habe diesen Fehler festgefahren. Ich habe auch andere ähnliche Probleme überprüft. Ich habe eine einfache Winkel/Feder-Anwendung. Die Anfrage gibt eine 200, aber ich erhalte eine JSON-Parse-Ausnahme in den Entwickler-Tools/Firebug für die Antwort. Die Antwort vom Server ist eine einfache Zeichenfolge, die ich sehen kann. Ich vermute das Problem ist, dass eckig es als JSON analysiert. Bitte hilf mir dabei.Fehler: JSON.parse: unerwartetes Zeichen in Zeile 1 Spalte 1 der JSON-Daten von Json
Winkelregler:
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);
});
}
};
}]);
Frühling Controller:
@RequestMapping(value = "/dologin", method = RequestMethod.POST)
public @ResponseBody String doLogin(@RequestBody final User user) {
String validateUser = "admin";
return validateUser;
}
Fehler msg:
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</[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
Benutzerklasse:
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;
}
}
Und wie genau sieht diese "einfache Zeichenfolge" aus? Oh warte, ich sehe es. Nun, der Text 'admin' ist definitiv kein gültiger JSON. Ein einfacher String-Wert muss in Anführungszeichen gesetzt werden. – Pointy
Bitte posten Sie beide JSON-Daten und die Benutzerklasse –
validateUser = "admin"; ist String, kein Json-Objekt. –