java - Ajax HTTPServlet 请求不会将信息从 ajax 传递到 Controller

标签 java ajax servlets

我的 java 端 Controller 没有收到我用 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();
    }

关于java - Ajax HTTPServlet 请求不会将信息从 ajax 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62324749/

相关文章:

java - Eclipse 中有不同颜色的 System.out.println() 语句吗?

javascript - 使用带有ajax和jquery的api调用初始化物化自动完成数据集

php - 在 jquery 中添加 ajax 后 OnClick 不起作用

javascript - 通过 javascript AJAX 发送 Java 对象

java - Java Servlet 和数据库连接错误

Java-为什么我的代码没有产生正确的结果?

java - SOAPHandler:如何从子元素中删除自动添加的命名空间/属性

java - HashMap 中的工厂方法对象存在访问问题

ajax - xmlHttpRequest abort() 方法不会关闭 Internet Explorer 中的连接

mysql - 插入之前检查数据库中已有的数据