当我将以下代码添加到我的html页面以获取java webapp时,页面上的脚本将退出响应。下面删除的代码段:
$("#btnCreateIng").click(function () {
alert("clicked");
var newName = document.getElementById("t_ingNameCreate").value;
var newCategory = document.getElementById("t_ingCategoryCreate").value;
var theData = "name=" + newName + "&" + "category=" + newCategory;
alert("Sending: " + theData);
var theUrl = "http://localhost:8080/webserv1/resources/ws2/ingredients/";
$.ajax({
url: theUrl,
type: "POST",
dataType: "text",
contentType: "application/x-www-form-urlencoded",
data: theData,
success: function (result, status, jqxhr) {
alert("success" + status);
var ingArray = JSON.parse(result);
alert(ingArray);
var output = "<h3>Just Added</h3>";
output += "<ul>";
for (var i = 0; i < ingArray.length; i++) {
output += "<li>" + ingArray[i].name + " (" + ingArray[i].id + "," ingArray[i].category + ")" + "</li>";
}
output += "</ul>";
alert(output);
$("#p_createIng").html(output);
},
error: function (xhr, status, errorthrown) {
alert("error" + status + " e: " + errorThrown);
$("#p_createIng").html("Error:" + xhr.status + " " + xhr.statusText);
}
});
});
允许脚本实际运行。我知道错误来自这里,但我无法找出错误。请帮忙。
答案 0 :(得分:1)
output+="<li>" + ingArray[i].name+ "("+ingArray[i].id + "," + ingArray[i].category + ")"+"</li>";
你忘了一个'+'号。 当然,如果在此语法错误修复后这不起作用 - 提供更多详细信息(请检查控制台)。