【发布时间】:2013-11-19 06:06:34
【问题描述】:
我正在尝试发布对 /draft 的请求并创建一个新的“草稿”/更新我数据库中的现有草稿。之后我想立即重定向到 /draft?id=RELEVANT_ID_HERE 页面。
这是我当前的 POST 请求函数:
app.post('/draft', function(req,res){
var id = req.query.id;
var postTitle = req.body.head;
var article = req.body.article;
if(id){
db.postCatalog.findOneAndUpdate({_id: id}, {title:postTitle, inShort:article.substring(0,100), content:article}, function(err, data){
if (err) {
return handleError(err);
}
else {
console.log(data);
res.status(200).redirect('/draft?id='+id);
}
});
}
else{
id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
new db.postCatalog({title:postTitle,
_id:id,
author:'temp',
AuthorID:2,
date:'2/3/12',
inShort:article.substring(0,100),
content:article ,
published:false
}).save(function (err, data) {
if (err) {
return handleError(err);
}
else {
console.log(data);
res.status(200).redirect('/draft?id='+id);
}
});
}
});
所以,除了重定向之外,一切正常。我在节点控制台中获得了正确的 GET 请求,但浏览器中没有任何反应。
这是 GET 请求的代码:
app.get('/draft', function(req,res){
var id = req.query.id;
if(id){
db.postCatalog.findOne({_id: id}, function(err, post){
if(err) {
return handleError(err);
}
else{
if(post){
console.log(post);
res.status(200).render('editDraft.hbs', {post: post});
}
else{
routes._404(req,res);
}
}
});
}
else{
console.log('creating new draft');
res.status(200).render('editDraft.hbs');
}
});
我正在使用 Express 和 Mongoose。视图引擎是 Handlebars。
感谢阅读!
【问题讨论】:
-
尝试返回状态 301 或 304 而不是 200。w3.org/Protocols/rfc2616/rfc2616-sec10.html
-
同样的问题。你解决了吗?我得到 127.0.0.1 - - [Sun, 01 Dec 2013 04:19:07 GMT] "GET /profile/12345 HTTP/1.1" 304 - "localhost:3004" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36" 但页面从未真正刷新
-
不。无论我尝试什么,都会得到相同的结果。我一直在寻找人们做同样事情的例子,但它只对他们有用。这非常令人沮丧。