このエラーが発生しました。他の同様の問題もチェックしました。私は単純なangularjs/springアプリケーションを持っています。リクエストでは200が返されますが、レスポンス用の開発ツール/ firebugにはJSON解析例外があります。サーバーからの応答は、私が見ることができる単純なStringです。問題は角度がJSONとして解析していることが原因だと思う。これで私を助けてください。エラー:JSON.parse:JSONデータの1行目の予期しない文字がJSONから
角度コントローラ:
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</[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
Userクラス:あなたは応答タイプを設定することができ
String validateUser = {"id": "admin"}
「単純な文字列」のように見えるのはどういうことでしょうか?ああ待って、私はそれを参照してください。テキスト 'admin'は間違いなく有効なJSONです。単純な文字列値は二重引用符で囲む必要があります。 – Pointy
JSONデータとユーザークラスの両方を投稿してください。 –
validateUser = "admin"; jsonオブジェクトではなくStringです。 –