-1

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.

Half_Duplex
  • 5,102
  • 5
  • 42
  • 58

1 Answers1

0

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.

Jai
  • 74,255
  • 12
  • 74
  • 103
  • The struts form tag takes care of the form name, so the data var is correct when debugging. – Half_Duplex May 27 '14 at 20:16
  • @Half_Duplex see js/jquery works on client side and you have not posted that your form has the name attribute when page gets loaded in the browswer. If `name="form_name"` is available at the client side in the browser _(you can test to see via firebug/inspector or see the page source)_ – Jai May 28 '14 at 06:48
  • @Half_Duplex see you have tagged this question with `jQuery` so as i have enough knowledge to answer to any question, i would like to share that ajax is a way to get the content without reloading/refreshing the page so that is neccessary. Also you have not responded well to the question asked about your post to get to the issue of error. – Jai May 28 '14 at 13:14