小编Mik*_*yan的帖子

类型错误:无法获取

我有一个由 GET 渲染的 index.pug 文件,以及将数据返回到 json 中的 Post。

router.get('/', function(req, res, next) {
  res.render('index', { title: 'agrotaxi' });
});

router.post('/', async function(req, res) {
......
res.json(info);
});
Run Code Online (Sandbox Code Playgroud)

问题是,当您尝试获取结果时,我得到了:

Uncaught (in promise) TypeError: Failed to fetch
Run Code Online (Sandbox Code Playgroud)

我的提取功能:

   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);
       });
   }
Run Code Online (Sandbox Code Playgroud)

不明白我做错了什么。 …

javascript fetch node.js

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

fetch ×1

javascript ×1

node.js ×1