jQuery 提交不处理“成功”
我已经浏览了在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除
success
函数声明后面的分号:success
函数声明是对象的一部分,对象之间用逗号分隔声明。Remove the semi-colon after the
success
function declaration:The
success
function declaration is part of an object, which separates declarations by comma.