【发布时间】:2017-06-05 08:33:45
【问题描述】:
我正在尝试使用 POST 命令向我的 servlet 填充表格,但我的网页没有发送 POST 请求,而是向 servlet 发送了 GET 请求。这是我的
$(document).ready(function() {
$.get("league", function(responseJson) {
if (responseJson != null) {
$("#tableleague").find("tr:gt(0)").remove();
var table1 = $("#tableleague");
$.each(responseJson, function(key, value) {
var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
rowNew.children().eq(0).text(value['teams']);
rowNew.children().eq(1).text(value['playedgames']);
rowNew.children().eq(2).text(value['wongames']);
rowNew.children().eq(3).text(value['tiegames']);
rowNew.children().eq(4).text(value['lostgames']);
rowNew.children().eq(5).text(value['scoredgoal']);
rowNew.children().eq(6).text(value['scoredgoal']);
rowNew.children().eq(7).text(value['scoredgoal']);
rowNew.appendTo(table1);
});
}
});
});
function addData() {
var t1 = $("#firstteam").val();
var t2 = $("#secondteam").val();
var s1 = $("#score1").val();
var s2 = $("#score2").val();
$("#firstteam").val('');
$("#secondteam").val('');
$("#score1").val('');
$("#score2").val('');
var data = {
firstteam: "t1",
secondteam: "t2",
score1: "s1",
score2: "s2"
}
$.ajax({
type: "POST",
url: "league",
data: JSON.stringify(data),
contentType: "application/x-www-form-urlencoded",
dataType:'json',
success: function(data, textStatus, jqXHR) {
if (data.success) {
$("#error").html("<div><b>Success!</b></div>" + data);
} else {
$("#error").html("<div><b>Information is Invalid!</b></div>" + data);
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Something really bad happened " + textStatus);
$("#error").html(jqXHR.responseText);
}
});
}
<h1>League Table</h1>
<table cellspacing="0" id="tableleague" style="width:100%">
<tr>
<th scope="col">Teams</th>
<th scope="col">Won</th>
<th scope="col">Tie</th>
<th scope="col">Lost</th>
<th scope="col">Scored</th>
<th scope="col">Received</th>
<th scope="col">Points</th>
</tr>
</table>
<form method="POST">
<div class="form_group">
<label for="FirstTeam">FirstTeam:</label>
<input type="text" class="form_control" id="firstteam">
</div>
<div class="form_group">
<label for="Score1">Score1:</label>
<input type="text" class="form_control" id="score1">
</div>
<div class="form_group">
<label for="Score2">Score2:</label>
<input type="text" class="form_control" id="score2">
</div>
<div class="form_group">
<label for="SecondTeam">SecondTeam:</label>
<input type="text" class="form_control" id="secondteam">
</div>
<div class="form_group">
<button type="submit" class="addbutton" id="addbut" onclick="addData();">Add match</button>
</div>
</form>
<div id="error"></div>
这是我在按下“添加匹配”按钮后立即在调试器中看到的内容:
我在控制台收到的错误是这个:
POST http://localhost:8080/league/league500(内部服务器错误)
我的代码做错了什么?我尝试了很多解决方案,但都没有奏效。
我的 servlet 代码:
@WebServlet("/league/*")
public class PopulateTable extends HttpServlet {
private static final long serialVersionUID = 1L;
public PopulateTable()
{
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
ArrayList<League> league = new ArrayList<League>();
league= Controller.getAllController();
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(league, new TypeToken<List<League>>() {}.getType());
JsonArray jsonArray = element.getAsJsonArray();
response.setContentType("application/json");
response.getWriter().print(jsonArray);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try {
PrintWriter out = response.getWriter();
JsonObject data = new Gson().fromJson(request.getReader(), JsonObject.class);
int s1 = Integer.parseInt(data.get("score1").getAsString());
int s2 = Integer.parseInt(data.get("score2").getAsString());
String t1 = data.get("firstteam").getAsString();;
String t2 = data.get("secondteam").getAsString();;
int ok = Controller.update(t1, t2, s1, s2);
JsonObject myObj = new JsonObject();
if(ok == 1)
{
myObj.addProperty("success", true);
}
else {
myObj.addProperty("success", false);
}
out.println(myObj.toString());
out.close();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
}
【问题讨论】:
-
instead of sending a POST request, my webpage sends a GET request你正在使用$.get()...? -
您检查过您的服务器错误日志吗?
500 Internal Server Error表示您的 JSP 端有问题,可能无法保存/插入数据。您需要检查日志。 -
我使用 .get 作为我的第一个函数,在我第一次加载页面时填充表格,但是当我按下添加匹配按钮时,我的页面应该发送一个 AJAX POST 请求,它不会不要这样做。
-
更改按钮类型
-
更改了按钮类型,我看到了一个全新的世界。看起来我的错误在 servlet 中,int s1 变量给了我一个 java.lang.NumberFormatException。