Я пытаюсь научиться экспресс и фронт JavaScript. Я пытаюсь передать данные JSON через почтовый запрос с API выборки и хочу взять на бэкэнд с экспресс.
Мой бэкэнд-код выглядит так:
const express = require('express');
const app = express();
const path = require('path');
app.get('/log', function(req, res){
console.log("Hi");
});
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'index.html')));
app.listen(3000, () => console.log('Example app listening on port 3000!'));
и мой файл index.html выглядит так:
<!DOCTYPE html>
<html>
<head>
<title>Fetch And Express Test</title>
</head>
<body>
<input id="a">
<input id="b">
<button> Click</button>
<script>
document.getElementsByTagName("button")[0].addEventListener("click",function(){
console.log("Here");
let aInput = document.getElementById("a").value;
let bInput = document.getElementById("b").value;
let json = {"a": aInput, "b": bInput};
var data = new FormData();
data.append( "json", JSON.stringify(json));
fetch('http://localhost:3000/log', {
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, same-origin, *omit
headers: {
"Content-Type": "application/json; charset=utf-8",
// "Content-Type": "application/x-www-form-urlencoded",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: data, // body data type must match "Content-Type" header
})
.then(function(res){});
});
</script>
</body>
</html>
Проблема здесь в том, что он не регистрирует «Привет», тогда как если я удаляю второй параметр функции fetch и отправляю только запрос get, все работает нормально. В чем проблема?