java - 无法在 jQuery 中使用 $post 将数据发送到 servlet

标签 java jquery jsp servlets

我正在尝试通过自定义 jQuery 确认框将值传递给 servlet。

我在网上搜索,发现我们可以使用 $post 将数据传递给 servlet,但不知何故,数据没有传递给 servlet。

我的要求是我需要向用户提供两个选项来打开保存 pdf文档。我使用带有这些选项的自定义 jQuery 框,但数据没有传递到 servlet。

下面是我的代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.2/themes/redmond/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.11.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<!-- User Defined Js file -->
<script type="text/javascript">

$(document).ready(function(){
          $('#Export').click(function(event) {  
          event.preventDefault();
        var currentForm = $(this).closest('form');

        var dynamicDialog = $('<div id="conformBox">'+
        '<span style="float:left; margin:0 7px 20px 0;">'+
        '</span>Open or save the document</div>');

        dynamicDialog.dialog({
                title : "Open/Save Dialog",
                closeOnEscape: true,
                modal : true,

               buttons : 
                        [{
                                text : "Export",
                                click : function() {
                                                                        $(this).dialog("close");
 $.POST({
                type: "post",
                url: "button",
                data: $('#Export').attr("name"),
                  success: function(data) {
                    $('#results').show();
                    $('#results').html(data);
                }  
            });
          // currentForm.submit(); // if I use this then control going to servlet but data is not passed without nothing was happening



                                }
                        },
                        {
                                text : "Open",
                                click : function() {
                                        $(this).dialog("close");
                                        currentForm.submit();
                                }
                        }]
        });
        return false;
    });

}); 

</script>
</head>
<body>
     <form id="god" action="button">
        <button type="button" id="Export">Export</button>
     </form>
     <div id="results"></div>
</body>
</html>

Servlet 代码:

package com.testcase.testing;

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

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

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

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

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

        response.getWriter().append(request.getParameter("data"));
        System.out.println(request.getParameter("data"));


    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Testcase</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

<!--   <servlet>
    <servlet-name>button</servlet-name>
    <servlet-class>com.testcase.testing.button</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>button</servlet-name>
    <url-pattern>/Buttonpage.jsp</url-pattern>
</servlet-mapping> -->

</web-app>

无法追踪我做错了什么。

最佳答案

这是如何使用 $.post 发送数据

var param = "value";
$.post('query.php', { param: param }, function(data) {
    //do what you want with returned data
})

关于java - 无法在 jQuery 中使用 $post 将数据发送到 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44161928/

相关文章:

java - HttpGet 发送请求时编码错误

c# - 需要使用 jQuery getJSON 的工作示例

javascript - 每日特定时间倒计时

jquery - 如何使用 jQuery 选择 &lt;input type ="text"> 的值?

javascript - 加载 bootstrap 4 css 时,侧边栏导航折叠显示空白屏幕

java - 在 JSP 中生成 Excel 并通过电子邮件发送工作表

java - 解释Jsoup中NodeVisitor接口(interface)的head和tail方法

java - 错误: -Xdiag and String cannot be converted to int

java - getParameterValues 有时会从隐藏输入中返回 null

javascript - 将 json 响应重定向到 Ajax PUT 到 JSP 中以供 EL 解析