Javascript TypeError:获取失败

Javascript TypeError:获取失败,javascript,node.js,fetch,Javascript,Node.js,Fetch,我有一个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)

我有一个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.log(data);
       });
   }
不明白我做错了什么。

可能是由于某些原因导致提取失败,并且您没有捕获错误,因此,请尝试以以下方式添加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.log(data);
}).catch((error) => {
  console.log(error);
});

补充说,您也有同样的问题。您是否启用了NodeJS上的CORS以允许您的前端网页域?