1

Ajax code:

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: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>

Controller Class

@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;
}

Modal Class

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;
    }

}

I can't get the values textbox values or attachment in controller. The ObservationModal is always null

3 Answers 3

1

To make the ajax call the url must be of the type '/projectName/actualurl'. In your case url:'/projectName/updateObservation'. And also add dataType:'text' to the call.

Sign up to request clarification or add additional context in comments.

Comments

1

A file cannot be uploaded using AJAX. To make it happen you can use formdata for fileupload but this work for only html5 supported browsers

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

And if you want it to work even for older browsers you can use iframe with form for fileupload.

Comments

0

For uploading a file normally you need to use encType="multipart/form-data" in your form. If you want to use Ajax to upload a file, Along with Simple ajax call you need to use its fileupload plugin. For more details have a look here: Link1, Link2, Link 3, Link 4

1 Comment

Did you use fileupload plugin for ajax? Follow the given links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.