Я пытался сделать простую запись ajax formdata на сервер nodejs ...
const http = require('http');
const { parse } = require('querystring');
let
count = 1,
log = function ( body ) {
let bd = parse(body);
console.log(Date.now());
console.log(++count);
console.log(bd);
};
http.createServer((req, res) => {
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
log(body);
res.end("Ok");
});
} else {
res.end(`
<!doctype html>
<html>
<body>
<form action="/" method="post" onsubmit="return go(this);">
<input type="text" name="f01" /><br />
<input type="text" name="f02" /><br />
<input type="text" name="f03" /><br />
<button>Send</button>
</form>
<script>
function go(f) {
let
request = new XMLHttpRequest(),
formData = new FormData(f);
request.onload = function ( ret ) {
alert(ret.target.responseText);
}
request.open('POST', f.action);
request.send(formData);
}
</script>
</body>
</html>
`);
}
}).listen(3000);
Проблема в том, что req.on ('end') всегда вызываетсяв два раза больше---------------- 7e31ba3a10238 \ r \ nContent-Disposition: form-data;name ':' "f01" \ r \ n \ r \ na \ r \ n ----------------------------- 7e31ba3a10238 \r \ nContent-Disposition: данные формы;Name = "F02" \ г \ п \ г \ пь \ г \ п ----------------------------- 7e31ba3a10238 \ г \nContent-Disposition: форма-данные;Name = "F03" \ г \ п \ г \ пс \ г \ п ----------------------------- 7e31ba3a10238 - \r \ n '}
Второй раз в порядке:
[Объект: нулевой прототип] {f01:' a ', f02:' b ', f03:' c '}
Что я делаю не так?