亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

發送的 axios post api 數據在服務器端的 console.log(req.body)

發送的 axios post api 數據在服務器端的 console.log(req.body)

婷婷同學_ 2023-07-14 16:13:14
我已經從nodejs開始,并一直嘗試使用axios在post api中發送數據,但沒有在服務器端獲取它,這是我的設置客戶export default class App extends Component {  componentDidMount() {    const headers = {      'Content-Type': 'application/json',      'Authorization': 'JWT fefege...'    }    let data = { title: "abc", price: 20 }; // i am sending this data     axios      .post("http://localhost:5000/add-product", data, {        headers: headers,      })      .then((res) => console.log(res))      .catch((err) => console.log(err));  }   render() {    return (<div></div>);  }}服務器端const express = require("express");const bodyParser = require("body-parser");const mongoose = require("mongoose");const app = express();let cors = require("cors");const productRoutes = require("./routes/product");app.use(cors());app.use(bodyParser.urlencoded({ extended: false }));app.use(productRoutes);app.listen(5000)產品routes.js 文件const express = require('express');const productController = require('../controllers/product');const router = express.Router();router.post('/add-product', productController.addProduct);module.exports=router控制器文件 exports.addProduct = (req, res, next) => {console.log(req.body) //gives me {} everytime }在這里,我每次從客戶端發送一個對象,我將 {} 視為控制臺我的兩個項目分別運行在 localhost:3000 和 5000 上
查看完整描述

1 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

添加

app.use(bodyParser.json());


查看完整回答
反對 回復 2023-07-14
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號