【问题标题】:AJAX call from a popup modal来自弹出模式的 AJAX 调用
【发布时间】:2016-10-14 22:47:39
【问题描述】:

尝试从模式弹出窗口调用 AJAX 请求。

function exportReport(){
$.ajax({
        url : 'exportReport',
        data : {
            from : fromDate,
            to : toDate
        },
        success : function(responseText) {
            alert("Success");
        }
    });
    }

小服务程序:

@WebServlet("/exportReport")
public class GenerateReportServlet extends HttpServlet {

}

main.jsp:

<%@ page language="java" session="false" 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>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<head>
....................
<div class="modal fade" id="myModal" role="dialog">
<button type="button" class="btn btn-primary btn-sm" data-dismiss="modal" onclick="exportReport()">Export</button>
</div>
....................
</html>

main.jsp 在webapps\views 中。此导出按钮位于弹出模式 div 中。点击导出正在调用,

http://localhost:8080/myApp/views/exportReport?from=1465972200000&amp;to=1465107600000 是 HTTP 状态 404。它应该像 http://localhost:8080/myApp/exportReport?from=1465972200000&amp;to=1465107600000/views/ 是如何进入网址的。

【问题讨论】:

    标签: jquery ajax servlets bootstrap-modal


    【解决方案1】:

    设置url时要更具体,否则会附加到当前url。

    在 AJAX 调用中使用 url : '/myApp/exportReport'

    前导斜杠表示该url是绝对的

    省略前导斜杠使 url 相对到当前 url。

    【讨论】:

    • 它的工作。但是没有生成报告。但是当我手动将 url 粘贴到浏览器中时,它的生成似乎很奇怪.. 检查了 servlet。
    • 必须是服务器端的东西,例如如何接收数据。尝试将 ajax method 设置为 GETdataType 设置为 html。有关更多/替代 ajax 属性,请参阅 API:api.jquery.com/jquery.ajax
    猜你喜欢
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    相关资源
    最近更新 更多