如何从单个 id 页面向服务器发送 POST 请求 - Node JS

时间:2021-05-05 13:09:08

标签: node.js express ejs

我有一个包含表单的“单一产品”页面。我需要向服务器发送一个 POST 请求,以便我可以将所说的“单一产品”添加到购物车中。当我尝试向服务器发送 POST 请求时,在浏览器中遇到以下错误:

无法发布/single-product/api/single/cart/products

ejs 文件中的表单如下所示:

<form action="api/single/cart/products" method="POST" onsubmit="addedToCart()">
        <input hidden value="<%= product.id %>" name="productId" />
        <input hidden value="<%= product.title %>" name="productTitle" />
        <input hidden value="<%= product.price %>" name="productPrice" />
        <button class="button has-icon is-inverted"
        style="background-color: #f15d8a;"
        >
          <i class="fa fa-shopping-cart"></i>&nbsp; Add to cart
        </button>
      </form>

表单动作的接收端如下所示:

router.post("/single/cart/products", async (req, res) => {
const cookie = req.cookies;
const email = cookie.userEmail;
let cart;
const userId = await new AuthController().getId(cookie.userEmail);
let items = [req.body.productId, req.body.productTitle, req.body.productPrice]
let totalPrice = req.body.productPrice

await new CartController().record(userId, items, totalPrice, cookie.userEmail);

res.redirect('back');
});

我该如何处理这个错误?

0 个答案:

没有答案