【问题标题】:post request works with postman but not in browser, via axios发布请求适用于邮递员,但不适用于浏览器,通过 axios
【发布时间】:2018-09-18 13:58:12
【问题描述】:

我正在开发一个 MERN 应用程序。当我通过 Postman 向"/collections" 发出发布请求时,我得到了预期的响应,并且数据库使用新文档进行了更新。当我在浏览器中发出请求时,代码运行,但数据库没有获得新文档。这是我的提交处理程序(reactjs):

handleSubmit = async e => {
    e.preventDefault();
    try {
      await axios.post("/collections", this.state.data);
    } catch (error) {
      console.log(error.message);
    }
    this.props.history.push("/collections");
  }; 

当我提交相关表单时,该代码的最后一行运行,我的浏览器返回到"/collections"。代码不会抛出或记录任何错误,但提交不会创建新文档,正如 mongodb Compass 所揭示的那样。同样奇怪的是,这段代码是从应用程序的另一部分修改的,在浏览器中运行良好:

handleSubmit = async e => {
    e.preventDefault();
    await axios.post("/entries", this.state.data);
    this.props.history.push("/entries");
  };

这很好用,但发给"/collections" 的帖子不行。有什么帮助吗?

作为参考,这是节点中的发布路线:

router.post("/", async (req, res) => {
  try {
    const newCollection = await Collection.create(req.body);
    res.send(newCollection);
  } catch (error) {
    res.send(error.message);
  }
});

【问题讨论】:

  • 您应该在浏览器中打开开发工具并告诉我们请求发生了什么

标签: reactjs post axios


【解决方案1】:

感谢 Pedro Brost,我想通了。在开发工具中的“网络”下,由于我的猫鼬模式的设置方式,我收到了验证错误。我不知道“网络”是开发工具中的一个东西。

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 2021-10-06
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2020-11-08
    • 2019-11-09
    相关资源
    最近更新 更多