POST请求返回index.html正文,而不是适当的响应

编程入门 行业动态 更新时间:2024-10-07 22:27:18

POST请求返回index.html正文,<a href=https://www.elefans.com/category/jswz/34/1771316.html style=而不是适当的响应"/>

POST请求返回index.html正文,而不是适当的响应

我一直在开发一个Web应用程序,当前端通过代理托管在本地(托管在heroku上)时,所有帖子请求均可以正常工作。但是,当在Firebase上部署前端时,请求仍会成功返回,但不是{success:true}而是从构建目录返回index.html文件,并且不会对数据库执行任何操作。

heroku上的相关代码:

const app = express();
const port = process.env.PORT || 8080;

app.use(bodyParser.json());
app.use(cors());
app.use(express.static(path.join(__dirname, 'build')));
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", '*');
  res.header("Access-Control-Allow-Credentials", true);
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json');
  next();
});

app.post('/updateBoard', async (req, res) => {
  const { board_id, itemName, color, index, gameInfo } = req.body;
  await functions.updateBoard(board_id, itemName, color, index, gameInfo)
  res.send({ success: true })
})

app.get('*', function (req, res) {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

app.listen(port, () => console.log(`App listening on port ${port}!`));

各自的前端功能:

const updateBoardData = async (
    board_id,
    itemName,
    color,
    index,
    gameInfo
  ) => {
    var res = await fetch("/updateBoard", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        board_id: board_id,
        itemName: itemName,
        color: color,
        index: index,
        gameInfo: gameInfo,
      }),
    });
    console.log(res);
  };

已部署的输出:<!doctype html><html lang="en"><head> ... </html>

来自本地主机的输出:{“成功”:true}

回答如下:

好吧,我设法通过将获取中的路由设置为nameofproject.herokuapp/updateBoard来解决了这个问题,因为一旦部署了该应用程序后,该代理显然不起作用。

更多推荐

POST请求返回index.html正文,而不是适当的响应

本文发布于:2024-05-07 11:34:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   正文   POST   index   html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!