Javascript 404未找到(API url未找到,即使它100%存在)
我正在尝试通过id获取产品。现在,当我与邮递员一起尝试时,它工作得非常好,没有任何问题。但是,当我试图用Angular获取数据时,它没有工作,它一直说404找不到,我不知道是什么问题。如果有人知道,请告诉我。任何帮助都将不胜感激。这是我的密码 express.js: 路线:Javascript 404未找到(API url未找到,即使它100%存在),javascript,node.js,express,angular11,Javascript,Node.js,Express,Angular11,我正在尝试通过id获取产品。现在,当我与邮递员一起尝试时,它工作得非常好,没有任何问题。但是,当我试图用Angular获取数据时,它没有工作,它一直说404找不到,我不知道是什么问题。如果有人知道,请告诉我。任何帮助都将不胜感激。这是我的密码 express.js: 路线: router.get("/get-product/:id", async (req, res) => { await fetchProductById(req, res); }); UTIL:
router.get("/get-product/:id", async (req, res) => {
await fetchProductById(req, res);
});
UTIL:
const fetchProductById = async (req, res) => {
const id = req.params.id;
const prod = await Product.findOne({_id: id});
if (prod) {
if (prod.length == 0)
res.status(200).json({
message: "No Products",
success: true,
});
else {
res.status(200).json({
prod,
success: true,
});
}
} else {
res.status(400).json({
prod,
message: "couldn't find any product",
id: req.id,
success: false,
});
}
};
角度:
现在是角度服务:
getProductById(id: any){
return this.http.get<Products[]>(
environment.api + "products/get-product?id="+id
)
}
您使用了查询参数而不是url参数。它应该是
“products/get product/”+id
:
getProductById(id:any){
返回this.http.get(
environment.api+“产品/获取产品/”+id
)
}
let id = this.route.snapshot.params.id;
this._product.getProductById(id).subscribe(
(data: Products[])=>{
console.log(data)
},
error => {
console.log("there has been an error trying to fetch this product: "+id)
}
)