jQuery 提交不处理“成功”

发布于 2025-01-03 04:44:33 字数 1441 浏览 2 评论 0原文

我已经浏览了在 Stack Overflow 和 Google 上可以找到的所有解决方案,但似乎没有一个有帮助。

我在 Clojure(Noir 框架)中有一个函数,它接受两个键“text”和“day-of-note”并将值插入数据库。无论这是否有效,该函数都会返回带有 {"result":true} 的 JSON 响应(用于测试目的)。

(defpage [:post "/newpost"] {:keys [text day-of-note]}
  []
  (println "newpost called")
  (post text)
  (response/json {:result true}))

我的表单是一个简单的表单,带有一个文本区域、一个复选框和一个按钮。

<form action="/newpost" id="new-post" method="post">
  <textarea id="entry" name="text">Insert todays happenings</textarea>
    <br />
  <input checked="checked" name="day-of-note" type="checkbox" value="true">
  <input type="submit" value="Add entry">
</form>

提交表单时,我添加了一个警报调用,以向我显示 dataString 的内容,并且它们的格式正确(“text=lalala&day-of-note=true”)。

$(function () {
  $("#new-post").submit(function (e) {
    e.preventDefault();
    var dataString = $("#new-post").serialize();
    alert(dataString);
    $.ajax({
      url: "/newpost",
      type: "POST",
      dataType: "json",
      data: dataString,
      success: function () {
          alert("Success!");
      };
    });
    return false;
  });
});

当代码如上面所示时,当用户单击按钮并且页面显示 {"result":true} 时,会发生对 /newpost 的 HTML 调用。如果我注释掉“$.ajax”部分,则会弹出包含正确内容的消息框,但如果删除注释,则不会出现消息框,而是直接转到 /newpost。

我认为应该发生的是 /newpost 页面永远不会被呈现,但 Ajax 会对其进行对 dataString 的调用,并显示一个带有“成功!”的消息框。将显示。

我哪里走错路了?

I've gone through all of the solutions I could find on Stack Overflow and Google but none of them seem to help.

I have a function in Clojure (Noir framework) that takes two keys, "text" and "day-of-note" and inserts the values into a database. Regardless of whether or not that works, the function returns a JSON response with {"result":true} (for testing purposes).

(defpage [:post "/newpost"] {:keys [text day-of-note]}
  []
  (println "newpost called")
  (post text)
  (response/json {:result true}))

My form is a simple form with one textarea, a checkbox and a button.

<form action="/newpost" id="new-post" method="post">
  <textarea id="entry" name="text">Insert todays happenings</textarea>
    <br />
  <input checked="checked" name="day-of-note" type="checkbox" value="true">
  <input type="submit" value="Add entry">
</form>

When submitting the form I have added a call to alert to show me the contents of dataString and they are formatted correctly ("text=lalala&day-of-note=true").

$(function () {
  $("#new-post").submit(function (e) {
    e.preventDefault();
    var dataString = $("#new-post").serialize();
    alert(dataString);
    $.ajax({
      url: "/newpost",
      type: "POST",
      dataType: "json",
      data: dataString,
      success: function () {
          alert("Success!");
      };
    });
    return false;
  });
});

What happens here when the code is as it is above, there is a HTML call to /newpost when the user click on the button and the page shows {"result":true}. If I comment out the "$.ajax"-part the message box pops up with the correct content, but if I remove the comments -- no message box, just goes straight to /newpost.

What I thought was supposed to happen was that the /newpost page would never be rendered but a call with the dataString would be put to it by Ajax and a message box with "Success!" would be shown.

Where am I taking the wrong turn?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2025-01-10 04:44:33

删除 success 函数声明后面的分号:

success: function () {
    alert("Success!");
}

success 函数声明是对象的一部分,对象之间用逗号分隔声明。

Remove the semi-colon after the success function declaration:

success: function () {
    alert("Success!");
}

The success function declaration is part of an object, which separates declarations by comma.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文