我正在尝试使用JQuery / Ajax将一些数据从JSP发送到Servlet。以下是我的JSP文件的重要项目。
script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
var form = $('#customItemForm');
form.submit(function () {
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
success: function (data) {
}
});
return false;
});
</script>
<!--add custom item -->
<div class="modal fade" id="customItemModel" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"> <a href="#" data-dismiss="modal"> <img src="images/arrow-back-512.png" width="30px" height="30px"> <small>Back</small></a> <span id="myModalLabel" style="margin-left:20px;"><font size="+2"><b>Add Custom Item</b></font></span> </div>
<div class="modal-body">
<form class="form-horizontal" name="customItemForm" method="post" action="PastSurgicalCustomItem">
<fieldset id="modal_form">
<!-- Text input-->
<div class="form-group">
<label class="col-md-1 control-label" for="textinput">Name</label>
<div class="col-md-8">
<input id="customName" name="customName" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<div class="modal-footer">
<input type="submit" id="additional" class="btn btn-primary" data-dismiss="modal" value="Submit">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</fieldset>
</form>
</div>
</div>
</div>
<!-- /add custom item -->
以下是Servlet类
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PastSurgicalCustomItem extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String customName = request.getParameter("customName");
System.out.println(customName);
}
// <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);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
但是servlet不打印值,这意味着JQuery没有传递任何东西。我在这做错了什么?
答案 0 :(得分:0)
在 doGet 方法中设置断点并逐步执行代码以查看它是否完全执行或是否已执行,但会引发异常。
此外,您应该在浏览器中监控您的AJAX呼叫(例如,通过查看所有现代浏览器中可用的开发人员视图中的网络连接)并查看返回的AJAX呼叫的标头和内容。