gpt4 book ai didi

java - $.ajax JSON 检索失败 - JSP

转载 作者:行者123 更新时间:2023-12-01 09:58:26 25 4
gpt4 key购买 nike

我正在尝试从 Servlet 接收 JSON 数组并将数据添加到表中。但是 JSON 或文本不会出现在 jsp 中。我已经尝试了很多事情。这是我的 JSP

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Search a User</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#search1').click(function ()
{
var searchdata = $("#searchword").val();
$.ajax({
type: "GET",
url: "Search", //this is my servlet
data: {searchword :searchdata },
success: function(data){
alert(data);
}
});
});

});
</script>
</head>
<body>
<form method="post" class="form-horizontal">
<fieldset>


<div class="form-group">
<label class="col-md-4 control-label" for="searchword">Search a User</label>
<div class="col-md-5">
<input id="searchword" type="text" placeholder="Add User's name" class="form-control input-md" required="">

</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-3">
<button id="search1" class="btn btn-block btn-success">Search</button>
</div>
</div>

</fieldset>
</form>
</body>
</html>

这里我只是尝试确保成功功能正常工作。但警报不起作用。我添加了没有字符串的警报,我们从 Servlet 中检索以进行演示建议。但他们也没有奏效。我很确定我的错误出在 JSP 端,因为我可以看到 JSON 数组和 Servlet 在 log4j 日志中检索搜索词。但为了确保我也会在此处添加 servlet。

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import hsenid.DBConnector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Search extends HttpServlet {
private static final Logger logger = LogManager.getLogger(Search.class);


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

resp.setContentType("application/json");
PrintWriter out = resp.getWriter();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String username = req.getParameter("searchword");
DBConnector dbpool = (DBConnector) getServletContext().getAttribute("DBConnection");
JsonArray jsonArray = new JsonArray();
logger.info(username);
try {
Connection myConn = dbpool.getConn();
String likeQuery = "Select * from userdetails WHERE username LIKE ?";
preparedStatement = myConn.prepareStatement(likeQuery);
preparedStatement.setString(1, "%" + username + "%");
resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("firstName", resultSet.getString("fname"));
jsonObject.addProperty("lastName", resultSet.getString("lname"));
jsonObject.addProperty("dob", resultSet.getString("dob"));
jsonObject.addProperty("country", resultSet.getString("country"));
jsonObject.addProperty("email", resultSet.getString("email"));
jsonObject.addProperty("mobile", resultSet.getString("mnumber"));
jsonObject.addProperty("username", resultSet.getString("username"));

jsonArray.add(jsonObject);

}
logger.info("JSON ARRAY Created");

logger.info(jsonArray.toString());
resp.getWriter().write(jsonArray.toString());
out.println(jsonArray);
} catch (SQLException e) {
logger.error(e.getMessage());
}

}
}

最佳答案

您将结果 JSON 数组写入两次,因此内容不是有效的 JSON 响应,JQuery 将无法解析它:

resp.getWriter().write(jsonArray.toString());
out.println(jsonArray); // out is resp.getWriter()

因此只需删除第二行即可。

为了更好地诊断此类错误,您还应该向 $.ajax 调用添加一个 error 回调,并查看开发者控制台是否显示任何错误。

关于java - $.ajax JSON 检索失败 - JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37000552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com