编辑 :
总结问题:当我第一次使用 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]=', '' ] } }
说真的,这里到底发生了什么?这甚至有可能吗?
非常感谢您的任何帮助!
编辑:
如果我在提交一次表单后刷新页面,我会收到数十个“标头已发送”错误,分布在不同的 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 提交之前刷新页面,我永远不会收到这些错误。