【问题标题】:Fetch Delete Request not working Properly获取删除请求无法正常工作
【发布时间】:2018-05-08 13:07:11
【问题描述】:

使用 Fetch API 我发送以下请求:

const target = e.currentTarget;
fetch(target.href, {
  method: 'delete',
})
.then(res => console.log(22))
.catch(err => console.log(err));

另外,这是处理这个请求的中间件:

exports.deleteImageById = async (req, res) => {
 const image_id = req.params.image_id;
  const imagePromise = Image.findByIdAndRemove(image_id);
  const commentPromise = Comment.remove( {image_id} );
  await Promise.all([imagePromise, commentPromise])
    .catch(err => console.log(err));
  req.flash('Image Deleted!');
  // return something well!
  res.status(200);
};

正在删除文档,但 fetch 语句中的 then 块不起作用。它不会向控制台输出任何内容。

我在这里做错了吗?

【问题讨论】:

    标签: javascript node.js ajax express fetch


    【解决方案1】:

    没有收到响应,因此不会调用then。在服务器更改

    res.status(200);
    

    包含end(),因为status 不发送响应

    res.status(200).end();
    

    sendStatus

    res.sendStatus(200);
    

    【讨论】:

    • 如果我使用window.location.href = '/'req.flash 不起作用。我必须在客户端创建闪存吗?
    • 我不确定。看起来在重定向之前使用了 flash。你可以再问一个关于 flash 本身的问题!
    【解决方案2】:

    res.status 仅用于设置状态码它不发送响应。

    改为使用以下内容:

    res.sendStatus(200)

    【讨论】:

      猜你喜欢
      • 2014-06-08
      • 2011-06-14
      • 2014-11-03
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      相关资源
      最近更新 更多