为什么这个 ajax post 请求在 Nodejs express 中执行 post 请求之前使用数据执行 GET 请求?
Why is this ajax post request executing a GET request with the data right before the post request is executed in Nodejs express?
我这里有一个有趣的问题。这是我想使用 ajax 对服务器执行 post 请求的表单。
replyPrefix = "<div id='addCommentContainer'><form class='addCommentForm' name='addcomment' id='addCommentForm'>" +
"<div class='input-group'>" +
"<input class='form-control' class='commentContent' type='text' placeholder='Comment!' name='commentContent'>" +
"<input class='form-control' class='commentParent' type='hidden' name='parent' value='";
replySuffix = "'>" +
"<span class='input-group-btn'>" +
"<button class='btn btn-danger' type='submit' class='submitButton'>submit</button>" +
"</span>" +
"</div></form></div>";
(回复的值插入到此表单的 prefix/suffix 之间。请注意,在我尝试移动到 ajax 之前,此表单和代码正在执行 post 请求)
这是我执行 ajax post 请求的 jquery
$('.addCommentForm').submit(function() {
$.ajax({
type: "POST",
url: "/addcomment",
data: $(this).serialize(),
success: function(data) {
console.log("success:");
//alert(data);
},
error: function(e) {
console.log("error:");
}
});
});
这里是 nodejs express 代码。
// handle add comment call
router.post('/addcomment', function(req, res) {
//var obj = {};
console.log('body: ' + JSON.stringify(req.body));
//res.send(req.body);
//return;
var db = req.db;
var collection = db.get('comments');
// grab parent id if available
var parent = req.body.parent;
var content = req.body.commentContent;
console.log("parent: " + parent);
console.log("content: " + content);
// if no parent available, add new bubble
if (!parent || parent == "") {
console.log("Adding a bubble...");
addBubble(db, collection, content);
}
// otherwise, add comment to tree
else {
console.log("Adding a comment...");
addComment(db, collection, content, parent);
}
// DEBUG
//console.log(parent);
//console.log(req.body.commentContent);
//res.redirect('/');
});
通过 ajax 的 post 请求正在正确执行,回复被添加到数据库和所有内容,但是当 post 请求是因为每次提交表单时都会在它之前发送这个 get 请求而执行。
body: {"commentContent":"asdf","parent":"55f778aab671e4b41d05c6a7"}
parent: 55f778aab671e4b41d05c6a7
content: asdf
Adding a comment...
GET /?commentContent=asdf&parent=55f778aab671e4b41d05c6a7 200 65.626 ms - 53620
POST /addcomment - - ms - -
(以上是控制台输出,我的问题是为什么我提交的数据会被执行?如何防止这种情况发生?)
感谢您的帮助。
您似乎正在提交表单和 ajax 请求。尝试添加以下内容以防止默认提交操作 (GET)。
.submit(function(e) {
e.preventDefault();
//ajax code
}
我这里有一个有趣的问题。这是我想使用 ajax 对服务器执行 post 请求的表单。
replyPrefix = "<div id='addCommentContainer'><form class='addCommentForm' name='addcomment' id='addCommentForm'>" +
"<div class='input-group'>" +
"<input class='form-control' class='commentContent' type='text' placeholder='Comment!' name='commentContent'>" +
"<input class='form-control' class='commentParent' type='hidden' name='parent' value='";
replySuffix = "'>" +
"<span class='input-group-btn'>" +
"<button class='btn btn-danger' type='submit' class='submitButton'>submit</button>" +
"</span>" +
"</div></form></div>";
(回复的值插入到此表单的 prefix/suffix 之间。请注意,在我尝试移动到 ajax 之前,此表单和代码正在执行 post 请求)
这是我执行 ajax post 请求的 jquery
$('.addCommentForm').submit(function() {
$.ajax({
type: "POST",
url: "/addcomment",
data: $(this).serialize(),
success: function(data) {
console.log("success:");
//alert(data);
},
error: function(e) {
console.log("error:");
}
});
});
这里是 nodejs express 代码。
// handle add comment call
router.post('/addcomment', function(req, res) {
//var obj = {};
console.log('body: ' + JSON.stringify(req.body));
//res.send(req.body);
//return;
var db = req.db;
var collection = db.get('comments');
// grab parent id if available
var parent = req.body.parent;
var content = req.body.commentContent;
console.log("parent: " + parent);
console.log("content: " + content);
// if no parent available, add new bubble
if (!parent || parent == "") {
console.log("Adding a bubble...");
addBubble(db, collection, content);
}
// otherwise, add comment to tree
else {
console.log("Adding a comment...");
addComment(db, collection, content, parent);
}
// DEBUG
//console.log(parent);
//console.log(req.body.commentContent);
//res.redirect('/');
});
通过 ajax 的 post 请求正在正确执行,回复被添加到数据库和所有内容,但是当 post 请求是因为每次提交表单时都会在它之前发送这个 get 请求而执行。
body: {"commentContent":"asdf","parent":"55f778aab671e4b41d05c6a7"}
parent: 55f778aab671e4b41d05c6a7
content: asdf
Adding a comment...
GET /?commentContent=asdf&parent=55f778aab671e4b41d05c6a7 200 65.626 ms - 53620
POST /addcomment - - ms - -
(以上是控制台输出,我的问题是为什么我提交的数据会被执行?如何防止这种情况发生?)
感谢您的帮助。
您似乎正在提交表单和 ajax 请求。尝试添加以下内容以防止默认提交操作 (GET)。
.submit(function(e) {
e.preventDefault();
//ajax code
}