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>


控制器类

@RequestMapping(value = "/updateObservation", method = RequestMethod.POST)
public @ResponseBody String updateObservation(
        @ModelAttribute("ObservationModal") ObservationModal formObj) {
    String result = "";

    System.out.println(":" + formObj.getTooth());
    System.out.println(formObj.getAttachment());

    return result;
}


模态类

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始终为null

最佳答案

要进行ajax调用,URL必须为“ / projectName / actualurl”类型。在您的情况下,URL:'/ projectName / updateObservation'。并且还将dataType:'text'添加到调用中。

09-10 00:52
查看更多