我的控制器在java端没有接收到我用AJAX发送的信息,一开始是可以的,但是现在不行了,它一直返回null。
我试过:改变参数名将参数名放在""中,在Ajax中直接调用从注册表获取信息的方法。
信息是用Javascript进来的,并发送一个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);
}
}
ajax请求的类型是post,所以在servlet中你应该把你的代码放在doPost方法中,像这样。
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();
}