简体   繁体   English

无法使用jQuery中的$ post将数据发送到servlet

[英]Unable to send data to servlet using $post in jQuery

I am trying to pass a value to servlet through a custom jQuery confirmation box. 我试图通过自定义jQuery确认框将值传递给servlet。

I searched the web and found that we can use $post to pass the data to servlet but somehow data was not getting passed to servlet. 我在网上搜索后发现,可以使用$post将数据传递给servlet,但是某种程度上数据没有传递给servlet。

My requirement is I need to provide two options to user to open or save the pdf document. 我的要求是我需要向用户提供两个选项来opensave pdf文档。 I used custom jQuery box with these options but data is not getting passed to servlet. 我使用带有这些选项的自定义jQuery框,但数据没有传递到servlet。

Below is my code: 下面是我的代码:

<%@ 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 code: 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 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>

Unable to trace what I am doing wrong. 无法跟踪我在做什么错。

This is how to send data using $.post 这是使用$ .post发送数据的方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM