【问题标题】:Error: JSON.parse: unexpected character at line 1 column 1 of the JSON data fromJson错误:JSON.parse:JSON 数据 fromJson 的第 1 行第 1 列出现意外字符
【发布时间】:2016-06-21 18:08:57
【问题描述】:

我一直坚持这个错误。我也检查了其他类似的问题。我有一个简单的 angularjs/spring 应用程序。该请求给出了 200,但我在开发人员工具/firebug 中收到 JSON 解析异常以获取响应。服务器的响应是一个简单的字符串,我可以看到。我怀疑问题在于 Angular 将其解析为 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
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

用户类别:

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


}

【问题讨论】:

  • 那个“简单字符串”到底是什么样的?哦,等等,我看到了。那么文本admin 绝对不是有效的JSON。纯字符串值必须用双引号引起来。
  • 请同时发布 JSON 数据和用户类
  • validateUser = "admin";是字符串,不是 json 对象。
  • validateUser = "{\"name\":\"admin\"}";也许有效
  • 问题出在您的 Spring 方面。查看 spring 如何返回字符串标题帖子类型

标签: javascript angularjs json spring


【解决方案1】:

您可以在请求中使用config object 显式设置响应类型。这将覆盖响应标头中的 Content-Type 所假定的处理。

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

最终的解决方案是更改您的服务器端,使其不返回标题中带有 Content-Type: application/json 的响应。

【讨论】:

  • 我根据@Ali 的建议设置了一些类似的...我将控制器更改为:@RequestMapping(value = "/dologin", method = RequestMethod.POST, produces="text/plain") 并且能够完成这项工作..非常感谢您的帮助!
【解决方案2】:

字符串admin 不是有效的json,因此解析它会引发错误

尝试返回类似:

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

【讨论】:

  • 我也想过类似的问题,但有没有办法简单地读取角度字符串响应?
  • 检查docs.angularjs.org/api/ng/service/$http设置 HTTP 标头部分
  • 我已经检查过了...难道标题只是请求的更改不响应吗?
猜你喜欢
  • 1970-01-01
  • 2021-09-14
  • 2016-01-30
  • 2020-01-11
  • 2020-10-13
  • 2019-05-11
  • 1970-01-01
  • 2017-05-18
  • 2020-06-07
相关资源
最近更新 更多