2016-06-21 57 views
1

我一直卡在這個錯誤上。我也檢查了其他類似的問題。我有一個簡單的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 

enter image description here

enter image description here

用戶等級:

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


} 
+1

那「簡單的字符串」究竟是什麼樣子?哦,等一下,我明白了。那麼'admin'文本肯定是無效的JSON。一個普通的字符串值必須用雙引號括起來。 – Pointy

+0

請同時發佈JSON數據和用戶類 –

+0

validateUser =「admin」;是字符串,而不是json對象。 –

回答

2

您可以設置響應類型在請求中明確使用config object。這將覆蓋響應頭中的Content-Type所假定的處理。

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

最終解決方案雖然是改變你的服務器端,以便它不返回頭與Content-Type: application/json的響應。

+0

我在@ Ali的建議中設置了一些類似於此的...我將控制器更改爲:@RequestMapping(value =「/ dologin」,method = RequestMethod.POST,生成=「text/plain」)'做這個工作..謝謝你的幫助! – k19

1

字符串admin不是有效json,所以解析它拋出一個錯誤

嘗試好像回到了一句:

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

我也在類似的線路上想過,但是沒有辦法簡單地讀取角度中的字符串響應? – k19

+0

檢查https://docs.angularjs.org/api/ng/service/$http,第**節設置HTTP標題** –

+0

我已經檢查過了......沒有標題請求的更改只是沒有響應? – k19

相關問題