p19 p19 - 5 months ago 26
Javascript Question

Error: JSON.parse: unexpected character at line 1 column 1 of the JSON data fromJson

I have stuck on this error. I checked other similar problems as well. I have a simple angularjs/spring application. The request gives a 200 but I am receiving a JSON parse exception in the developer tools/firebug for the response. The response from the server is a simple String which I am able to see. I suspect the problem is that angular is parsing it as JSON. Please help me with this.

Angular controller:

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


Spring controller:

@RequestMapping(value = "/dologin", method = RequestMethod.POST)
public @ResponseBody String doLogin(@RequestBody final User user) {

String validateUser = "admin";

return validateUser;
}


Error msg:

Error: JSON.parse: unexpected character at line 1 column 1 of the JSON data
fromJson@http://localhost:8080/suite/extResources/Angular/angular.js:1352:9
defaultHttpResponseTransform@http://localhost:8080/suite/extResources/Angular/angular.js:10455:1
transformData/<@http://localhost:8080/suite/extResources/Angular/angular.js:10546:12
forEach@http://localhost:8080/suite/extResources/Angular/angular.js:322:11
transformData@http://localhost:8080/suite/extResources/Angular/angular.js:10545:3
transformResponse@http://localhost:8080/suite/extResources/Angular/angular.js:11343:21
processQueue@http://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.$eval@http://localhost:8080/suite/extResources/Angular/angular.js:17378:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:8080/suite/extResources/Angular/angular.js:17191:15
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:8080/suite/extResources/Angular/angular.js:17486:13
done@http://localhost:8080/suite/extResources/Angular/angular.js:11637:36
completeRequest@http://localhost:8080/suite/extResources/Angular/angular.js:11843:7
requestLoaded@http://localhost:8080/suite/extResources/Angular/angular.js:11776:1


enter image description here

enter image description here

User class:

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


}

Answer

You can set the response type explicitly using the config object in the request. This will override the processing being assumed by the Content-Type that is in the response's header.

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

Ultimately the solution though is to change your server-side so that it doesn't return a response with Content-Type: application/json in the header.

Comments