【问题标题】:Java Script Error Causing script to become unresponsiveJava 脚本错误导致脚本无响应
【发布时间】: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


【解决方案1】:
output+="<li>" + ingArray[i].name+ "("+ingArray[i].id + "," + ingArray[i].category + ")"+"</li>";

您忘记了一个“+”号。 当然,如果在此语法错误修复后这不起作用 - 请提供更多详细信息(检查控制台)。

【讨论】:

  • 哇,非常感谢你,这一直是我的错误。花了 3 个小时试图找到那个大声笑。
猜你喜欢
  • 1970-01-01
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 2017-10-08
  • 1970-01-01
  • 2022-11-04
相关资源
最近更新 更多