Java Spring MVC无法使用文件上传的Ajax表单提交

Java Spring MVC无法使用文件上传的Ajax表单提交,java,jquery,ajax,spring,spring-mvc,Java,Jquery,Ajax,Spring,Spring Mvc,Ajax代码: var str = $("#observationForm").serialize(); $.ajax({ type : "post", data : str, url : "updateObservation", async : true, /* dataType : "multipart/form-data", */ success : function() {

Ajax代码:

var str = $("#observationForm").serialize();

    $.ajax({
        type : "post",
        data : str,
        url : "updateObservation",
        async : true,
/* dataType : "multipart/form-data", */
        success : function() {
            alert("success");
        }
    });
JSP Spring表单:

<form:form modelAttribute="ObservationModal" action="updateObservation" id="observationForm">
    <label class="control-label">Tooth No</label>
    <input type="text" class="form-control" name="tooth" id="tooth" placeholder="Enter tooth no" />
    <label class="control-label">Uploaded file(PDF)</label>
    <input type="file" class="form-control" name="attachment" value="" id="attachment" placeholder="" />        
    <input type="button" value="Save" onclick="updateObservation();" />
</form:form>
模态类

public class ObservationModal implements Serializable {
    int tooth;
    private List<MultipartFile> attachment;

    public int getTooth() {
        return tooth;
    }

    public void setTooth(int tooth) {
        this.tooth = tooth;
    }

    public List<MultipartFile> getAttachment() {
        return attachment;
    }

    public void setAttachment(List<MultipartFile> attachment) {
        this.attachment = attachment;
    }

}
公共类ObservationModal实现可序列化{
内牙;
私人名单附件;
公共int getTooth(){
回牙;
}
公共齿(内齿){
这个。牙齿=牙齿;
}
公共列表getAttachment(){
返回附件;
}
公共文件附件(列表附件){
这个。附件=附件;
}
}

我无法在控制器中获取值文本框值或附件。ObservationModal始终为空

要进行ajax调用,url的类型必须为“/projectName/actualurl”。在您的示例中,url:“/projectName/updateObservation”。并向调用中添加数据类型:“text”。

不能使用AJAX上传文件。让它发生 您可以使用formdata进行文件上载,但这仅适用于支持html5的浏览器

var form = $('form')[0]; // You need to use standart javascript object here
var formData = new FormData(form);

如果您想让它甚至适用于较旧的浏览器,您可以使用iframe with form进行文件上载。

要上载文件,通常需要在表单中使用
encType=“multipart/form data”
。 如果您想使用Ajax上传文件,除了简单的Ajax调用外,还需要使用其fileupload插件。 有关更多详细信息,请查看此处:、,

您是否使用了用于ajax的fileupload插件?遵循给定的链接。
var form = $('form')[0]; // You need to use standart javascript object here
var formData = new FormData(form);