我喜欢这样:
客户端.js
var fd = new FormData();
fd.append('test', 'test');
$.ajax({
url: '/form',
data: fd,
processData: false,
type: 'POST',
headers: {
'Content-Type': undefined
},
success: function ( d ) {
console.log( d );
},
error: function( a, b, c){
console.log( a );
console.log( b );
}
});
服务器.js
var multiparty = require('multiparty');
var app = express();
...
app.post('/form', function(req, res) {
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
console.log('fields:', fields);
});
}
在日志中我得到:
字段:未定义
为什么不解析字段?
因为在 AJAX $.ajax({data:..}); 必须传递 urlencoded 数据,如 "?test=test" 。然后后端将接收它作为输入及其值。
你传递了表单对象。