提问人:MC_Vovi 提问时间:10/6/2023 最后编辑:marc_sMC_Vovi 更新时间:10/7/2023 访问量:49
Java servlet 不会将响应传递给 AJAX jQuery
Java servlet doesn't pass response to AJAX jQuery
问:
我尝试使用AJAX jQuery向servlet发送请求并显示带有结果的表,但是发送响应后我唯一看到的是
因此,正如我们所看到的,servlet 处理请求并形成响应,但在处理请求后它不会返回 js。
AJAX格式:
$(document).ready(function() {
$('#form').submit(function(event) {
event.preventDefault(); // Prevent the default form submission
// Get values from form elements
let x = $('#x').val();
let y = $('#y').val();
let r = $('#r').val();
// Send an AJAX GET request to the servlet
$.get('ControllerServlet', { val_x: x, val_y: y, val_r: r }, function(result) {
let res = result.split(", ");
let now = new Date();
let newData = { x: x, y: y, r: r, time: now, execution_time: res[0], result: res[1] };
console.log(x, y, r);
let newRow = '<tr>' +
'<td>' + newData.x + '</td>' +
'<td>' + newData.y + '</td>' +
'<td>' + newData.r + '</td>' +
'<td>' + newData.time + '</td>' +
'<td>' + newData.execution_time + '</td>' +
'<td>' + newData.result + '</td>' +
'</tr>';
$('#result-table tbody').append(newRow);
});
});
});
这是我的servlet代码:
@WebServlet("/ControllerServlet")
public class ControllerServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
CustomResponseWrapper wrapper = new CustomResponseWrapper(resp);
req.getRequestDispatcher("/AreaCheckServlet").include(req, wrapper);
String resultString = wrapper.getRes().getResult().toString();
String queryTimeString = wrapper.getRes().getQueryTime().toString();
resp.setContentType("text/plain");
resp.getWriter().println(queryTimeString + ", " + resultString);
resp.getWriter().close();
resp.getWriter().flush();
}
}
我尝试更改浏览器,将ajax重写为fetch api。但没有任何帮助,似乎问题出在我的 servlet 代码中。
答: 暂无答案
评论