简体   繁体   English

为什么 Fetch post 方法会给出 404 错误

[英]Why does Fetch post method is giving a 404 error

I'm creating a virtual store as part of my Bootcamp projects and ran into this problem when trying to get the confirmation page from the cart page using the fetch post method I get a 404 error.我正在创建一个虚拟商店作为我的 Bootcamp 项目的一部分,并在尝试使用 fetch post 方法从购物车页面获取确认页面时遇到了这个问题,我收到 404 错误。 Not sure if it's a server issue or my code.不确定是服务器问题还是我的代码。

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
  "firstName": "kk",
  "lastName": "41k421k",
  "address": "14125415",
  "city": "541521",
  "email": "515k215",
  "products": [
    "107fb5b75607497b96722bda5b504926"
  ]
});
var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};
fetch("http://localhost:3000/api/order", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

// product.js (routes) // product.js (路由)

 const express = require('express');
    const router = express.Router();
    const productCtrl = require('../controllers/product');
    router.get('/', productCtrl.getAllProducts);
    router.get('/:id', productCtrl.getOneProduct);
    router.post('/order', productCtrl.orderProducts);
    module.exports = router;

// app.js // 应用程序.js

const express = require('express');
const path = require('path');

const productRoutes = require('./routes/product');

const app = express();

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content, Accept, Content-Type, Authorization');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
  next();
});

app.use('/images', express.static(path.join(__dirname, 'images')));
app.use(express.static('images'));

app.use(express.urlencoded({extended: true}));
app.use(express.json());

app.use('/api/products', productRoutes);

module.exports = app;

// product.js (controller) // product.js (控制器)

exports.orderProducts = (req, res, next) => {
if (!req.body.contact ||
!req.body.contact.firstName ||
!req.body.contact.lastName ||
!req.body.contact.address ||
!req.body.contact.city ||
!req.body.contact.email ||
!req.body.products) {
return res.status(400).send(new Error('Bad request!'));
}
let queries = [];
for (let productId of req.body.products) {
const queryPromise = new Promise((resolve, reject) => {
Product.findById(productId).then(
(product) => {
if (!product) {
reject('Product not found: ' + productId);
}
product.imageUrl = req.protocol + '://' + req.get('host') + '/images/' + product.imageUrl;
resolve(product);
}
).catch(
() => {
reject('Database error!');
}
)
});
queries.push(queryPromise);
}

The request URL and the URLs in the server code don't match.请求 URL 和服务器代码中的 URL 不匹配。

The request is sent to http://localhost:3000/api/order but the server is listening for the post request at http://localhost:3000/api/products/order .请求被发送到http://localhost:3000/api/order但服务器正在http://localhost:3000/api/products/order处监听发布请求。

Change the fetch request to将获取请求更改为

fetch("http://localhost:3000/api/products/order", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM