使用ajax,jquery在spring中上传文件时出错?

时间:2016-02-24 07:46:08

标签: javascript jquery ajax spring

我在上传文件时遇到的问题是我的代码 这是我的表格,其中Plot Location Picture是一个文件字段

<form method="POST" enctype="multipart/form-data" id="plotData" action="${request.contextpath}/FrontEnd/resources/saveplot?${_csrf.parameterName}=${_csrf.token}">
    <fieldset>
        <table class="diffaddsocc">
            <input type="hidden" name="societyBlockId" />
            <tr>
                <td>
                    <div>
                        <label>Property Number:</label>
                        <input type="text" name="propertyNumber" id="adcssno" />
                    </div>
                </td>
                <td>
                    <div>
                        <label>Plot Size:</label>
                        <select name="size" id="sizeid321" class="sizeclass321"> 
                </select>
                    </div>
                    <div>
                        <label>Plot Front Face:</label>
                        <input type="text" name="plotFrontFace" id="adcssno" />
                    </div>
                </td>
                <td>
                    <div>
                        <label>Plot Location Picture:</label>
                        <input type="file" name="fileup" id="fileup" />

                    </div>
                </td>
                <td>
                    <div>
                        <label>Plot Location Picture Date:</label>
                        <input type="date" name="plotLocationPictureDate" />
                        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
                        <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
        </table>
    </fieldset>
</form>

这是我的ajax jquery函数,通过提交表单

来调用
$(function() {
         addPlot = $( "#plot-form" ).dialog({
              autoOpen: false,
              height: 790,
              width: 1300,
              modal: true,
              buttons: {
                  "Save": addPlotInfo,
                    Cancel: function() {
                        addPlot.dialog( "close" );
                  }
                },
                close: function() {
                    document.getElementById('plotData').reset();
                }
            });
            $( "#add-plot" ).button().on( "click", function() {
                $( '#plot-form input[name="societyBlockId"]' ).val(subSocietyId); 
                addPlot.dialog( "open" );
            });
          });
    function addPlotInfo()
    {


        var formData = new FormData(document.getElementById("plotData"));

        console.log(formData);


                        $.ajax({
                            url: 'saveplot',
                            type: 'post',
                            data: formData,
                            processData: false,
                            contentType: false,
                            success: function (response, status, xml) {
                                if (response == "success") {
                                    addPlot.dialog( "close" );
                                    document.getElementById('plotData').reset();
                                    loadBahriaTable();
                                } else {
                                    alert("There is an error while adding plot. Try again.");
                                }
                            }
                        });

    }

这是我的控制器方法

@RequestMapping(value = "/saveplot", method = RequestMethod.POST)
    public @ResponseBody String Save(HttpServletRequest request,SocietyBlock societyBlock,
            Plot plot,  @RequestParam("fileup") MultipartFile file, BindingResult bindingResult, Model model, 
            @RequestParam(required = false) Boolean reverseBeforeSave) {

        String plottype = request.getParameter("plottypes12");
        String plotstatus = request.getParameter("plotstatus12"); 
        String[] plotfeatures = request.getParameterValues("plotfeas");
        plot.setSocietyBlock(societyBlock);
        propertyService.addPlot(plot, plottype, plotstatus, plotfeatures);
        return "success";
    }

我已经在网上尝试了20多个30个教程,在stackoverflow中看到了很多问题,但它无法解决我的问题我总是得到这个错误

error: Feb 24, 2016 10:54:06 AM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver logException
WARNING: Handler execution resulted in exception: Request method 'POST' not supported

请让我知道如何删除此错误我浪费了我的6个小时但无法解决此错误..如果我的代码需要任何更改告诉我,如果有人为我解决此错误,那将是非常值得的??

0 个答案:

没有答案