编辑:
总结问题:当我第一次使用ajax发布内容时,它设置了req.body值,但是这个值永远不会重置,即使我再次加载页面(F5),或者我执行另一个POST请求:数据保持设置状态,并保持相同的值,并且永远不会再次更新(直到我重置服务器)
原始帖子:
我是使用Express.js的菜鸟。
我正在使用以下代码执行非常基本的AJAX表单提交。
sites_edit: function(req, res)
{
if (req.route.method == 'post')
{
//console.log('========== POST');
var oSite = req.param('site');
var SiteModel = models.Site;
//console.log(oSite);
//console.log(req.body);
var pCallback = function()
{
oSite.owner = app.getOwner();
new SiteModel(oSite).save(function(e)
{
//console.log('saved.');
//console.log(e);
var aRet = app.analyzeSave(e);
if (aRet.success)
aRet.site = oSite;
res.json(aRet);
});
};
if (oSite.id)
{
models.Site.findById(oSite.id, function(err, oTmpSite)
{
if (err)
throw new Error(err);
if (oTmpSite.owner != app.getOwner().id)
throw new Exception('bad owner');
pCallback();
});
}
else
pCallback();
return;
}
// Handling GET request here
// (...)
}
提交的数据如下:
site[id]:
site[name]:
site[address]:
site[postal_code]:
site[city]:
site[phone]:
site[email]:
问题是,当我第一次发布表单时,我得到(生成)缺少字段的错误。但是如果我再次提交它,我只会在Firebug的网络监控中获得一个事件,但是我得到两个新的node.js POST调用,包括“已发送的头”错误。此外,req.body中的数据会被堆叠。
我的意思是,我在控制台中获得以下输出:
Starting server in environment: local
Express app started on port 80
POST /configuration/site/enregistrer 200 9ms - 262
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:645:11)
at ServerResponse.res.setHeader (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\patch.js:59:22)
at next (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\proto.js:153:13)
at multipart (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\multipart.js:60:27)
at module.exports (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\bodyParser.js:57:9)
at IncomingMessage.module.exports (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\urlencode
d.js:68:11)
at IncomingMessage.EventEmitter.emit (events.js:90:17)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socket.ondata (http.js:1704:22)
POST /configuration/site/enregistrer 200 10ms - 128
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:645:11)
at ServerResponse.res.setHeader (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\patch.js:59:22)
at next (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\proto.js:153:13)
at multipart (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\multipart.js:60:27)
at module.exports (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\bodyParser.js:57:9)
at IncomingMessage.module.exports (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\urlencode
d.js:68:11)
at IncomingMessage.EventEmitter.emit (events.js:90:17)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socket.ondata (http.js:1704:22)
POST /configuration/site/enregistrer 200 7ms - 128
如果我调试req.body值,我得到:
// First AJAX submit
{ site:
{ id: '',
name: '',
address: '',
postal_code: '',
city: '',
phone: '',
email: '' } }
// Second AJAX submit
{ site:
{ id: '',
name: [ '', '' ],
address: [ '', '' ],
postal_code: [ '', '' ],
city: [ '', '' ],
phone: [ '', '' ],
email: [ 'site[id]=', '' ] } }
// And another POST request is (strangely) handled by Express.js during the 2nd AJAX submit, which leads to third dump of the (apparently updated) variable
{ site:
{ id: '',
name: [ '', '', '' ],
address: [ '', '', '' ],
postal_code: [ '', '', '' ],
city: [ '', '', '' ],
phone: [ '', '', '' ],
email: [ 'site[id]=', 'site[id]=', '' ] } }
说真的,到底发生了什么事?这甚至是遥远的吗?
提前感谢您的帮助!
修改
如果我在提交表单一次后刷新页面,我会收到DOZENS的“已发送标头”错误,在不同的HTTP请求中传播(用于获取静态js / css文件)。
如下错误:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:645:11)
at ServerResponse.res.setHeader (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\patch.js:59:22)
at next (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\proto.js:153:13)
at multipart (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\multipart.js:60:27)
at module.exports (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\bodyParser.js:57:9)
at IncomingMessage.module.exports (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\urlencode
d.js:68:11)
at IncomingMessage.EventEmitter.emit (events.js:123:20)
at Object.resume (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\node_modules\pause\index.js:25:18)
at resume (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\static.js:61:13)
at SendStream.error (E:\nodejs\projects\arent\node_modules\express\node_modules\connect\lib\middleware\static.js:73:37)
如果我在使用AJAX提交之前刷新页面,我绝不会遇到这些错误。
答案 0 :(得分:0)
很抱歉,但解决方案并不明显。
但你的expressjs代码很奇怪:我的方法处理通常类似于:
app.post('/enregistrer/:site', function (req, res) {
...
}
这样我就不必测试req.route.method
。
错误消息Can't set headers after they are sent.
表示您没有结束响应,并且下一个响应也是相同的。
但是,您使用res.json
,其行为应与res.send
相同。所以,应该发送响应。
或者,您在响应代码中使用res.send
ou res.json
两次,然后第二次再次发送标题?