0

My controller on java side is not receiving the information i send with my AJAX It worked in the beginning but now it does not work, it keeps returning null.

I've tried: Changing parameter name Putting parameter name in "" Calling methods that get the information from registrationform directly in Ajax

The information is coming in in Javascript, and is sending a Arraylist.

Javascript:

$("#registrationForm").submit(function() {
    $.ajax({
        type : 'POST',
        data : {
            "allInformation" : getAllCorrectInformation(),
            hasCode : $("#course_option-1").is(":checked"),
            fullPayment : $(".fullPayment").is(":checked"),
            allSecondInformation : getAllSecondInformation(),
        },
        url : 'FormController',
        success : function(result) {

        }
    });
    return false;
});

function getAllCorrectInformation() {
    var allMainInformation = [];
    if($("#course_option-1").is(":checked")) {
        allMainInformation.push($("#courseCode").val());
    }
    // Cursist
    allMainInformation.push($("#gender").val());
    allMainInformation.push($("#birthday").val());
    allMainInformation.push($("#firstName").val());
    allMainInformation.push($("#insertion").val());
    allMainInformation.push($("#lastName").val());
    // ALS KINDERVERSIE !!LATER AANPASSEN!!
    allMainInformation.push($("#parentsName").val());
    allMainInformation.push($("#addressNr").val());
    allMainInformation.push($("#zipCode").val());
    allMainInformation.push($("#email").val());
    allMainInformation.push($("#phoneNumber").val());
    // Payment
    if($(".fullPayment").is(":checked")) {
        allMainInformation.push($("#bank").val());
    } else {
        allMainInformation.push($("#nameAccountHolder").val());
        allMainInformation.push($("#iban").val());
    }
    return allMainInformation;
}

function getAllSecondInformation() {
    var allSecondInformation = [];
    $(".secondPersonContainer").each(function() {
        if($(this).css("display")!="none") {
            allSecondInformation.push($(this).find(".secondGender").val());
            allSecondInformation.push($(this).find(".secondBirthday").val());
            allSecondInformation.push($(this).find(".secondFirstName").val());
            allSecondInformation.push($(this).find(".secondInsertion").val());
            allSecondInformation.push($(this).find(".secondLastName").val());
        }
    });
    return allSecondInformation;
}

FormController.java

package houseoftyping.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import houseoftyping.domain.Registration;

/**
 * Servlet implementation class FormController
 */
@WebServlet("/FormController")
public class FormController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public FormController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");

        if(request.getParameterNames().hasMoreElements()) {
            System.out.println(request.getParameterNames().nextElement());
        }

        System.out.println(request.getParameter("allInformation[]"));
        List information = Arrays.asList(request.getParameter("allInformation").split(","));

        List secondInformation = Arrays.asList(request.getParameter("allSecondInformation").split(","));
        Registration registration = new Registration(information, Boolean.parseBoolean(request.getParameter("hasCode")), Boolean.parseBoolean(request.getParameter("fullPayment")), secondInformation);
        PrintWriter out = response.getWriter();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

LorenzoOtto
  • 112
  • 8
  • 1
    before sending values please check on your client side if correct value you are receiving in ajax or not .do `console.log()` or `alert` to debug.Also ,see if your browser console shows any error. – Swati Jun 11 '20 at 12:58
  • @Swati as I said above, the values in the javascript (client side) is correct. Somewhere between Javascript and Java it goes lost. – LorenzoOtto Jun 11 '20 at 13:17
  • check what datas are getting passed to server via ajax.i.e : check [this](https://stackoverflow.com/a/41767097/10606400) answer and [this](https://stackoverflow.com/questions/1820927/request-monitoring-in-chrome) as well – Swati Jun 12 '20 at 03:54

1 Answers1

0

The type of the ajax request is post so in the servlet you should put your code in the doPost method like this:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");

        if(request.getParameterNames().hasMoreElements()) {
            System.out.println(request.getParameterNames().nextElement());
        }

        System.out.println(request.getParameter("allInformation[]"));
        List information = Arrays.asList(request.getParameter("allInformation").split(","));

        List secondInformation = Arrays.asList(request.getParameter("allSecondInformation").split(","));
        Registration registration = new Registration(information, Boolean.parseBoolean(request.getParameter("hasCode")), Boolean.parseBoolean(request.getParameter("fullPayment")), secondInformation);
        PrintWriter out = response.getWriter();
    }
Mehdi Ziat
  • 71
  • 6