2016-06-21 14 views
1

このエラーが発生しました。他の同様の問題もチェックしました。私は単純な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 

enter image description here

enter image description here

Userクラス:あなたは応答タイプを設定することができ

String validateUser = {"id": "admin"} 
+1

「単純な文字列」のように見えるのはどういうことでしょうか?ああ待って、私はそれを参照してください。テキスト 'admin'は間違いなく有効なJSONです。単純な文字列値は二重引用符で囲む必要があります。 – Pointy

+0

JSONデータとユーザークラスの両方を投稿してください。 –

+0

validateUser = "admin"; jsonオブジェクトではなくStringです。 –

答えて

2

adminが有効jsonではありません

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; 
    } 


} 
+0

私は@ Aliの提案でこれに似たものをいくつか設定しました...コントローラをこれに変更しました: '@RequestMapping(value ="/dologin "、method = RequestMethod.POST、=" text/plain ")'この仕事をするために..あなたの助けをたくさんありがとう! – k19

1

文字列なので、パースはそれが

のようなものを返すときにエラーがスローされます要求にconfig objectを明示的に使用します。これにより、応答のヘッダーにあるContent-Typeによって想定されている処理が上書きされます。

$http.post('/suite/rest/dologin',formData,{responseType:'text'}) 

最終的に解決策は、しかし、それはヘッダー内Content-Type: application/jsonとの応答を返さないように、サーバー側を変更することです。

+0

私は同様の行でも考えましたが、単に文字列応答を角度で読み取る方法はありませんか? – k19

+0

https://docs.angularjs.org/api/ng/service/$http、セクション** HTTPヘッダーの設定** –

+0

私は既にこれをチェックしています...ヘッダーはリクエストのみではなくレスポンスに対する変更を行いません? – k19

関連する問題