SyntaxError:意外的标记S,AngularJS,Spring

时间:2015-04-28 13:44:37

标签: javascript angularjs spring

我正在建立一个必须有数据和徽标的简单注册。在测试中可以单独传输文件和数据,但在尝试将它们一起发送时会发生以下错误:

SyntaxError: Unexpected token S
at Object.parse (native)
at qc (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:14:245)
at Zb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:76:423)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:77:283
at r (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:7:302)
at Zc (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:77:265)
at c (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:78:414)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:112:113
at n.$get.n.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:126:15)
at n.$get.n.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:123:106)

角度控制器

angular.module('EntregaJaApp').controller('EstabelecimentoController', ['$scope', '$http','Upload', function($scope, $http,Upload){

$scope.salvar = function(){
         Upload.upload({
            url: 'http://localhost:8080/site-web/gerencial/estabelecimento/salvar',
            file: $scope.picFile[0],
            method: 'POST',
            headers: {'Content-Type': 'multipart/form-data'}, // only for html5
            data: {'estabelecimento': $scope.estabelecimento}
        });
}}

Spring Controller

@Controller
@RequestMapping("/gerencial/estabelecimento")
public class EstabelecimentoController {

@Autowired
private EstabelecimentoService estabelecimentoService;

    @RequestMapping(value = "/salvar",  method = RequestMethod.POST)
public ResponseEntity<?>  salvar(Estabelecimento estabelecimento,@RequestParam(value="file", required=false) MultipartFile file){
    try {
          byte[] bytes;

            if (!file.isEmpty()) {
                 bytes = file.getBytes();
                //store file in storage
            }

            System.out.println(String.format("receive %s from %s", file.getOriginalFilename(), estabelecimento.getNome()));
        estabelecimentoService.salvar(estabelecimento);
        return new ResponseEntity<>(MensagensGerais.SUCESSO_SALVAR,HttpStatus.OK);
    } catch (Exception e) {
        return new ResponseEntity<>((StringUtil.eVazia(e.getMessage()) ? MensagensGerais.ERRO_CONSULTAR : e.getMessage()),HttpStatus.BAD_REQUEST);
    }
}
   }

2 个答案:

答案 0 :(得分:0)

你错过了返回类型注释吗?喜欢

@RequestMapping(value = "/salvar",  method = RequestMethod.POST)
@Produces(MediaType.APPLICATION_JSON)
public ResponseEntity<?>  salvar(Estabelecimento estabelecimento,@RequestParam(value="file", required=false) MultipartFile file){...}

答案 1 :(得分:0)

假设请求指定了Accept Header“application / json”,似乎没有正确地序列化字符串(Spring?)。 1.3.x之前的Angular版本似乎很慷慨,但现在当响应不正确JSON时抛出异常。我已将以下回复transformer添加到我的应用中:

$httpProvider.defaults.transformResponse.unshift(function(data, headersGetter, status){
            var contentType = headersGetter('Content-Type');
            if(angular.isString(contentType) && contentType.startsWith('application/json')){
                try {
                    angular.fromJson(data);
                } catch(e){
                    var mod = '"'+data+'"';
                    try {
                        angular.fromJson(mod);
                        return mod;
                    }
                    catch(e){
                        return data;
                    }
                }
                return data;
            }
            else{
                return data;
            }
        });

通过将JS字符串包装到其他"中,它将JS字符串转换为JSON字符串对象。