繁体   English   中英

语法错误:意外的令牌S,AngularJS,Spring

[英]SyntaxError: Unexpected token S, 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}
        });
}}

弹簧控制器

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

您是否错过了返回类型注释? 喜欢

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

假设请求指定了一个接受标头“ 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字符串对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM