Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Axios post请求因404故障而失败_Javascript_Node.js_Express_Vue.js_Axios - Fatal编程技术网

Javascript Axios post请求因404故障而失败

Javascript Axios post请求因404故障而失败,javascript,node.js,express,vue.js,axios,Javascript,Node.js,Express,Vue.js,Axios,我正在使用Axios查询后端中的端点。当我尝试这样做时,我得到一个404找不到。如果我从控制台复制/粘贴它在错误中提供的uri,并尝试在浏览器中直接访问它,它连接良好,不会给我错误(而是给我一个预期的空对象) 下面是我的Axios代码 axios.post("/api/myEndpoint", { id: this.userID }) .then((response) => { this.property = response.data.p

我正在使用Axios查询后端中的端点。当我尝试这样做时,我得到一个404找不到。如果我从控制台复制/粘贴它在错误中提供的uri,并尝试在浏览器中直接访问它,它连接良好,不会给我错误(而是给我一个预期的空对象)

下面是我的Axios代码

axios.post("/api/myEndpoint", { id: this.userID })
      .then((response) => { 
        this.property = response.data.property;
      })
      .catch((errors) => {    
        console.log(errors);    
        router.push("/");    
      });
下面是我的后端中的路由定义

const myEndpointRoute = require('../api/myEndpoint.js')();
exprApp.use('/api/myEndpoint', myEndpointRoute);
作为参考,uri为'http://localhost:3000/api/myEndpoint'. 我可以在浏览器中完全访问此uri,但Axios返回404,如上所述。正是因为这个原因,我相信这是前端的一个问题,但是我已经以与我的许多其他请求相同的方式设置了这个Axios请求,它们都工作得很好

编辑:这是后端的其余部分

myEndpoint.js

module.exports = function() {
const express = require('express'), router = express.Router();
const authMiddleware = require('../loaders/authMiddleware.js')();

router.get('/', authMiddleware, async function(req, res) {
  const id = req.body.id;

  const property = await require('../services/myEndpointService.js') 
    (id).catch((e) => { console.log(e) });
    res.send({ property: property });
  });

  return router;
};
myEndpointService.js

module.exports = async function(id) {
  const results = await require('../models/getMyEndpointProperty')(id);

  return results;
};
getMyEndpointProperty

module.exports = async function(id) {
  const pool = require('../loaders/pool.js')();

  const res = await pool.query(`SELECT * FROM myTable WHERE id = ${id};`);
  return res.rows;
};
myEndpoint.js只定义了一个
GET
方法,但是您的axios调用会在前端发送一个
POST
。尝试更改(或添加)快速路线:

//注意`.post`
router.post('/',authMiddleware,异步函数(req,res){
...
})

由于这个原因,当您在浏览器中手动测试它时,它也能工作,因为浏览器发送了一个
GET
请求。

您能显示myEndpoint.js吗?我添加了一些额外的后端信息谢谢,我一定已经查看了该文件1000次,但没有注意到