为什么我对JAVA-Servlet的AJAX调用不起作用

时间:2019-09-12 08:39:04

标签: ajax

我不明白为什么AJAX-POST无法在我创建的动态Web项目上运行。我有一个servlet,可以使用其url进行调用,但是每当我对其进行AJAX调用时,doPost函数中的请求为空。

我以为AJAX调用中的URL是错误的,但是我尝试过的所有URL都无法解决问题。我知道在调试servlet时会调用它的doPost函数。

The servlet looks like this:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenproject.mave;

import java.io.BufferedWriter;
import java.io.*;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.persistence.*;
import java.util.*;

/**
 *
 * @author andfe
 */
public class DB extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet DB</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet DB at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        write(request);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>


    private void write(HttpServletRequest request) {

       try (FileWriter writer = new FileWriter("C:\\Users\\andfe\\Desktop\\log.txt");
         BufferedWriter bw = new BufferedWriter(writer)) {

        bw.write(request.getContextPath());

    } catch (IOException e) {
            e.printStackTrace();
    }


    }

}

这是我的JavaScript:

/**
 * This class is the controller for the main view for the application. It is specified as
 * the "controller" of the Main view class.
 *
 * TODO - Replace this content of this view to suite the needs of your application.
 */

Ext.define('app.view.main.MainController', {
    extend: 'Ext.app.ViewController',

    alias: 'controller.main',

    onItemSelected: function (sender, record) {
        Ext.Msg.confirm('Confirm', 'Are you sure?', 'onConfirm', this);
    },

    onConfirm: function (choice) {
        if (choice === 'yes') {  

        var xhttp = new XMLHttpRequest();
        xhttp.open("POST", "/mave/DB", true);
        xhttp.setRequestHeader("Content-type", "Content-Type: text/html; charset=utf-8");
        xhttp.send("test"); 

        }
    }
});

1 个答案:

答案 0 :(得分:0)

更改为:

xhttp.setRequestHeader("Content-type", "text/html; charset=utf-8");