Javascript SyntaxError:意外标记S、AngularJS、Spring

Javascript SyntaxError:意外标记S、AngularJS、Spring,javascript,angularjs,spring,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/a

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

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);
    }
}
   }
弹簧控制器

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);
    }
}
   }
@控制器
@请求映射(“/GERECIAL/estabelecimento”)
公共级物业管理人{
@自动连线
私人地产服务业地产服务业;
@RequestMapping(value=“/salvar”,method=RequestMethod.POST)
公共响应性salvar(Estabelecimento Estabelecimento,@RequestParam(value=“file”,required=false)多部分文件){
试一试{
字节[]字节;
如果(!file.isEmpty()){
字节=file.getBytes();
//在存储器中存储文件
}
System.out.println(String.format(“从%s接收%s”,file.getOriginalFilename(),estabelecimento.getNome());
EstablecimentoService.salvar(Establecimento);
返回新的响应状态(MensagensGerais.successo_SALVAR,HttpStatus.OK);
}捕获(例外e){
返回新的响应属性((StringUtil.eVazia(e.getMessage())?MensagensGerais.ERRO\u CONSULTAR:e.getMessage()),HttpStatus.BAD\u请求);
}
}
}

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

@RequestMapping(value = "/salvar",  method = RequestMethod.POST)
@Produces(MediaType.APPLICATION_JSON)
public ResponseEntity<?>  salvar(Estabelecimento estabelecimento,@RequestParam(value="file", required=false) MultipartFile file){...}
@RequestMapping(value=“/salvar”,method=RequestMethod.POST)
@产生(MediaType.APPLICATION_JSON)
公共响应性salvar(Estabelecimento Estabelecimento,@RequestParam(value=“file”,required=false)多部分文件文件){…}

假设请求指定了一个接受头“application/json”,字符串似乎没有正确序列化(由Spring?)。1.3.x之前的Angular版本似乎很慷慨,但现在当响应不正确时会引发异常。我已将以下响应
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字符串转换为JSON字符串对象,方法是将其包装在附加的

中,因为错误出现在解析中。我猜发送到Angular的数据无效,或者是预期的数据。您是否能够显示正在传输的内容?我正在尝试传输{“nome”:“Test”},还尝试了JSON.parse()和Angular.toJson(),但出现了相同的错误。检查您的JSON数据是否有效…您可以使用fiddler o wireshark查看服务器响应吗?响应头视图已解析HTTP/1.1 200 OK服务器:Apache Coyote/1.1内容类型:应用程序/JSON内容长度:17------WebKitformBoundarySka2p1hiuydeupmy内容处置:表单数据;name=“data”{“Establecimento”“:{nome:“aaa”}----WebKitFormBoundaryskA2p1hIUyDUePmY内容配置:表单数据;name=“file”;filename=“mito.jpg”内容类型:image/jpeg