0

So I am using this method to send my form data to a servlet:

function send() {
var formData = $("#myForm").serialize();
$.ajax({
    url: "generateutil",
    async: true,
    data: {
        formData: formData
    },
    dataType: 'text',
    success: function(data) {
        //do the do
    }
});
}

Using a form like this:

<form id="myForm" method="POST">
  <input  type="text" name="firstName" id="firstName"/>
</form>

My question is, how do I then GET this information from the servlet? Trying the regular request.getParameter("firstName") always yields null values. I suppose I have to do something with the formData first or something but I cannot find ANYTHING about this anywhere online, tons of information on how to pass the data, but none on how to actually retrieve it.

I suspect I am missing something very obvious or doing something wrong...

P.S: I can't simply submit the form normally for reasons I don't really want to get into here.

EDIT: I know the data is indeed sent to the server as I can see the whole thing in fire bug:
formData
firstName=MyName&lastName=MyLastName.....

Servlet code:

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");
    response.setHeader("Pragma","no-cache");
    response.setHeader("Cache-Control","no-cache");
    response.setDateHeader("Expires",0);
    request.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();

    String toPrint = "";
    toPrint += "First name: ";
    toPrint += request.getParameter("firstName");
    out.write(toPrint);
}

That's the gist of it anyway, there are dozens of other variables that I add up but it's more of the same and it'd take pages to fit the entire thing here. The problem is that request.getParameter("firstName"); returns null, as do all the other variables, even though I see them having an actual value in the parameter tab of firebug.

CoqPwner
  • 913
  • 1
  • 11
  • 24
  • Your ajax looks like a GET request, so `request.getParameter("firstName")` should be the correct way to retrieve values. There must be some simple mistake causing it to fail. Show us your servlet code. Verify that the parameters are indeed being sent to the server, perhaps by logging the values or examining the browser dev tools. – Eric Guan Aug 23 '16 at 15:40
  • @EricGuan see my edit – CoqPwner Aug 23 '16 at 16:03
  • I don't see anything obviously wrong. Try printing `request.getRequestURL().append('?').append(request.getQueryString());`. It should show your complete URI and query string. – Eric Guan Aug 23 '16 at 16:21
  • @EricGuan link?formData=firstName%3DMyName%26lastName%3DmyLastName%26.... Aside from encoding the =s and &s, it seems alright, is that the problem? – CoqPwner Aug 23 '16 at 16:33
  • Ah... now i understand. In your ajax call, don't assign `formData : formData` in your data object, it's already correctly formatted. You want the result to be `link?firstName=MyName&lastName=myLastName... etc`. When you assign the preformatted query string to formData, your result is `link?formData=firstName=MyName&lastName=myLastName&`, which is incorrect. Try `data:formData` – Eric Guan Aug 23 '16 at 16:39
  • @EricGuan /facepalm so simple! I was doing it that way because I had other parameters I wanted to pass along, but simply putting them in hidden inputs does the trick and everything works. Thank you very much for the help, good sir! – CoqPwner Aug 23 '16 at 16:59

0 Answers0