У меня есть файл index.pug, который отображается с помощью GET, и Post, который возвращает данные в json.
router.get('/', function(req, res, next) {
res.render('index', { title: 'agrotaxi' });
});
router.post('/', async function(req, res) {
......
res.json(info);
});
Проблема в том, что когда вы пытаетесь получить результат, я получаю:
Uncaught (in promise) TypeError: Failed to fetch
моя функция выборки:
document.getElementById('go').addEventListener('click',getdata);
function getdata() {
let start = document.getElementById('start').value;
let end = document.getElementById('end').value;
let options = {
"start": start,
"end": end
};
fetch('http://localhost:3000', {
method: 'post',
headers: {
"Content-type": "application/json; charset=UTF-8"
},
body:JSON.stringify(options)
})
.then(function(res){
return res.json(); //error here
})
.then(function(data){
console.info(data);
});
}
Не понимаю, что я делаю не так. картинка ошибки



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Возможно, проблема заключается в сбое выборки по какой-либо причине, и вы не фиксируете ошибку, поэтому попробуйте добавить метод catch следующим образом:
fetch('http://localhost:3000', {
method: 'post',
headers: {
"Content-type": "application/json; charset=UTF-8"
},
body:JSON.stringify(options)
}).then(function(res){
return res.json(); //error here
}).then(function(data){
console.info(data);
}).catch((error) => {
console.info(error);
});
Включили ли вы CORS на NodeJS, чтобы разрешить домен веб-страницы внешнего интерфейса?
Добавил, что у тоже есть такая же проблема.