【发布时间】:2014-05-04 07:50:34
【问题描述】:
当我将以下代码添加到 java webapp 的 html 页面时,页面上的脚本停止响应。以下删除时的 sn -p:
$("#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);
}
});
});
允许脚本实际运行。我知道错误来自这里,但我无法找出错误。请帮忙。
【问题讨论】:
-
请至少使用基本缩进以使您的代码可读。 (在这种情况下,我已经为您编辑了您的代码,添加了空格但没有更改任何实际文本。)无论如何,我看不出该代码如何减慢您的页面,因为它所做的只是绑定一个点击处理程序- 在您点击
#btnCreateIng元素之前,实际函数不会运行。 -
你确定
java标签在这里是相关的吗? -
你有很多警报调试器。代码能走多远?您是否收到来自服务器的响应?还是在那之前挂起?我怀疑 contentType 有问题:“application/x-www-form-urlencoded”...
标签: java javascript web-applications