SyntaxError:Object.parse(native)上的意外标记Y

时间:2015-06-05 22:55:15

标签: angularjs spring-boot

我使用spring boot和angular js上传文件,但是当我点击导入时,我有标题中提到的错误。

这是我的控制器js

    var app=angular.module("MyCat",[]);
    app .directive("fileread", [function () 
                    { return { scope: { fileread: "=" }, link: function (scope, element, attributes) 
                    { element.bind("change", function (changeEvent)

                            { scope.$apply(function () { scope.fileread = changeEvent.target.files[0];
                             }); }); } } }]);                                                                                                                                                                                                                                                   // } }
//Upload files
        $scope.upload=function(){

                var fd = new FormData();
                var url="http://localhost:8080/upload";
                fd.append("file", $scope.rap.file);
                fd.append("name",$scope.rap.name);
               $http.post(url,fd,{
                   transformRequest: angular.identity,
                   headers: {'Content-Type': undefined}
               })
               .success(function(){
               })
               .error(function(){
               });

        }

这是我的java控制器

upload Files

    @RequestMapping(value="/upload",headers=("content-type=multipart/*"),consumes = {"multipart/form-data"}, method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(@RequestParam("name") String name,@RequestParam("file") MultipartFile file){
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(new File(name)));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + "!";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }

这是我的html页面

<section id="contact-page">
    <div class="container">
        <div class="center">        

            <p class="lead">Import reports</p>
        </div> 
        <div class="row contact-wrap"> 
            <div class="status alert alert-success" style="display: none"></div>
            <form id="main-contact-form" class="contact-form" name="contact-form" method="POST" enctype="multipart/form-data" >
                <div class="col-sm-5 col-sm-offset-1">
                    <div class="form-group">
                        <label>name *</label>
                        <input type="text" name="name" class="form-control" required="required" ng-model="rap.name">
                    </div>
                   <div class="form-group">  
                        <label>DateOfUploade</label>
                        <input type="Date" class="form-control" ng-model="rap.dateOfUpload" >
                    </div>
                        <div class="form-group">
                            <label for="t">Type of File</label>
                            <select 
                                class="form-control" id="t" ng-model="rap.type">
                                <option>.word</option>
                                <option>.xsl</option>
                                <option>.pdf</option>
                            </select>
                        </div> 

                        <div class="form-group">
                        <label>file</label>
                        <input type="file" name="file" class="form-control" fileread="rap.file">
                    </div>                        

                    <div class="form-group">
                        <button type="submit"  class="btn btn-primary btn-lg"  ng-click="upload()">Import File</button>
                    </div>

                </div> 
                    <table class="table">
                        <tr>
                            <th>NameOfUploader</th>
                            <th>DateOfUpload</th>
                            <th>TypeOfFile</th>
                            <th>File</th>
                        </tr>
                        <tr ng-repeat="rap in reports">
                            <td>{ {rap.NameOfUploader}}</td>
                            <td>{ {rap.DateOfUpload}}</td>
                            <td>{ {rap.TypeOfFile}}</td>
                            <td>{ {rap.File}}</td>
                        </tr>
                    </table> 

                </form> 
        </div><!--/.row-->
    </div><!--/.container-->
</section><!--/#contact-page-->
enter code here

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

意外的标记'Y' - 从java控制器启动你的消息'你成功...'来自服务器而不是有效的JSON字符串。尝试返回可解析的内容:{"result": "You successfully bla-bla-bla..."}