0

Following jsp code works fine in the forntend with Form but I wanted to find out how to call the action using Jquery. In short I do not want to use action and directly invoke the same through jquery.

<form:form id="uploadCSVForm" modelAttribute="PeopleFileUpload" method="post" enctype="multipart/form-data" action="/admin/institutions/${mrceuser.person.institution.id}/uploadCsv">
        <p>
          <form:label for="fileData" path="fileData">File</form:label><br/>
          <form:input path="fileData" type="file"/>
        </p>
        <p>
          <input type="button" value="Save" onclick="InstitutionDialog.prepareForSubmit('#uploadCSVForm');" />
          <input type="button" value="Cancel" onclick="InstitutionDialog.hide();return false;"/>
        </p>
      </form:form>

I have achieved the same using

 ChangeDialog.noInputSubmit(ChangeDialog.InstitutionsProperty, "Uploaded CSV for " + jQuery('input#name').val(),
      function () {jQuery('#uploadCSVForm').submit()});

Where ChangeDialog.noInputSubmit looks like this :

ChangeDialog.noInputSubmit = function (changeArea, message, callback) {
    ChangeDialog.changeArea = changeArea.id;
    var valueMap = ChangeDialog.getValueMap();
    valueMap.description = message;
    jQuery.ajax({
        url:RootPath.getPath()+'/admin/changes/',
        type:'POST',
        data:valueMap,
        success:callback,
        error:ChangeDialog.handleError
    });
    return false;
};

1 Answer 1

1

If you want to submit the form using jquery only, then you need to prevent the submit event first then trigger the submit event on success as below :

$('#btnSubmit').click(function (e) {
e.preventDefault(); //  prevent default action of submit button
var element = this;    
ajax({
    url:RootPath.getPath()+'/admin/changes/',
    type:'POST',
    data:valueMap,
    contentType: "application/json; charset=utf-8",
    success: function (data) {
    if (data.status == "Success") {
        alert("Done");
        $(element).closest("form").submit(); // submit form
    } else {
        alert("Error occurs on the Database level!");
    }
    },
    error:ChangeDialog.handleError
});
});

You can change the method name, parameters as per your requirement, but the core logic should be same.

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

Comments

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.