代码之家  ›  专栏  ›  技术社区  ›  Nafis

未捕获的语法错误:在生产模式下GET请求的意外标记<

  •  0
  • Nafis  · 技术社区  · 7 年前

    我已经在Heroku(Node backend和React for frontend by create React app)中部署了我的程序。对于POST请求,它工作得很好,但是对于GET请求,我收到了

    未捕获的语法错误:意外的标记<

    我有这个密码应用程序.js对于前端文件夹中生成文件夹的绝对路径:

    let root = path.join(__dirname, 'front-end', 'build'); // (on Heroku ==>  
    path.join(__dirname, 'front-end', 'build'); )
    app.use(express.static(root));
    app.use(function(req, res, next) {
      if (req.method === 'GET' && req.accepts('html') && !req.is('json') &&!req.path.includes('.')) {
          res.sendFile('index.html', { root });
       } else next();
     });
    

    project folder

    提前谢谢你的帮助。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Nafis    7 年前

    我已经找到了在前端将头添加到fetch请求的答案,因为这里我正在检查

    因此,我在请求中添加了标题:

    fetch('/alladmins', { 
         headers: {"Content-Type": "application/json",
                    "Accept" : "application/json"}
          })
        .then(data => data.json())
        .then((data) => { this.setState({ arrayOfAdmins: data });
      }
      , 
      function (error) {
        console.error("Error with fetching /alladmins url:", error);
      });
    

    它现在起作用了,有点奇怪,因为 得到