Post form via ajax, Struts ActionForm is empty

1.8k Views Asked by At

I'm converting a JSP to use ajax to post to the action but the ActionForm is blank when the request comes through. I am not sure what is going on as this should be working fine from what I've read. If I remove the ajax the page submits just fine. UPDATE: When debugging, the data var shows the correct string for the form. Also I've added the Struts config.

HTML:

<html:form action="/saveSomething" method="POST"> ... </html:form>

JS:

$('form[name="form_name"]').submit(function(e) {
    e.preventDefault();
    var url = $('form[name="form_name"]').attr('action');
    var data = $('form[name="form_name"]').serialize();
    var posting = $.post(url, data);
});

Struts config:

<action name="form_name" path="/notesPage" scope="request" type="com.action.Action" validate="false" input="/jsp/notesPage.jsp">
          <forward name="success" path="/jsp/notesPage.jsp" />      
        </action>

This is Struts 1 and as I said, if I remove the JS form submit method everything works fine so I don't think it's my mapping or anything to do with Struts.

1

There are 1 best solutions below

3
On

Your form does not have a name attribute:

$('form[name="form_name"]')

so instead you can do this:

$('form').submit(function(e) {
    e.preventDefault();
    var url = $(this).attr('action'); // get the action of current context form
    var data = $(this).serialize(); // get the data of current context form
    var posting = $.post(url, data);
}); // <---this is the proper closing of submit function

And i hope you are putting this code in document ready block.