简体   繁体   English

req.body是空的,我无法弄清楚为什么

[英]req.body is empty and I can't figure out why

I'm new to express and node and I'm trying to make an axios post to my server but the req.body is coming up empty when I console.log in node. 我是表达和节点的新手,我正在尝试向我的服务器发布一个axios帖子,但是当我在节点中的console.log时,req.body将变空。 Anyone know what I'm doing wrong? 谁知道我做错了什么? I'm console logging and sending to postman. 我是控制台记录并发送给邮递员。

Here is server.js 这是server.js

const app = require('express')()
const bodyParser = require('body-parser');



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

app.get('/', (req, res) => {

  res.send('hello')
})

const port = 4444;

app.post('/send', (req, res) => {
      console.log('body: ', req.body)
      res.status(200).send(req.body)
});



app.listen(port, () => {
  console.log('We are live on port 4444');
});

My axios call 我的axios电话

export default class Form extends Component {
     constructor(props) {
         super(props) 
         this.state = {
            name: 'kaleb',
            message: '',
            email: '',
            number: '',
            sent: false
         }




     }

     handleChange = (e) => {
        this.setState({
            [e.target.name]: e.target.value
        })

     }

     handleSubmit = () => {
         axios.post("/send", {name: this.state.name}).then(() => {
             console.log(this.state.name)
         })


     }

try this In server.js: 在server.js中试试这个:

    app.use(bodyParser.json());
    app.use(bodyParser.json({type: 'application/*+json'}));
    app.use(bodyParser.urlencoded({extended: false}));//or true whatever you need

axios call: axios电话:

    axios({method:'post',url:'/send',data:{name:this.state.name}})
      .then(console.log(this.state.name));

Try putting the url as http://localhost:4444/send in your axios call: 尝试将url作为http://localhost:4444/send到你的axios调用中:

 handleSubmit = () => {
         axios.post("http://localhost:4444/send", {name: this.state.name}).then(() => {
             console.log(this.state.name)
         })

Ok, Here's what I think after read all of the above comments. 好的,这是我在阅读完所有上述评论后的想法。

  1. You said, when you send the request via postman it's console.log an empty req.body. 你说,当你通过邮递员发送请求时,它是console.log一个空的req.body。 So there's no problem with your URL. 所以你的网址没问题。 It's heading to correct router. 它正在改正路由器。 Postman have 3 content types. 邮差有3种内容类型。 Just select "X-www-form-urlencoded" & send the request again. 只需选择“X-www-form-urlencoded”并再次发送请求即可。 It should work. 它应该工作。
  2. In your component, You said nothing happen after submit. 在你的组件中,你说在提交后没有任何事情发生。 Most probably the problem should be with your URL. 最有可能的问题应该是你的网址。 Your backend server runs on PORT 4444 & usually react app run on PORT 3000. So when you call axios.post('/send') it's actually sent to localhost:3000/server instead of localhost:4444/server . 您的后端服务器在端口4444上运行并且通常会在PORT 3000上运行应用程序。因此,当您调用axios.post('/ send')时,它实际上发送到localhost:3000/server而不是localhost:4444/server Now it will not work. 现在它不会起作用。 You need to set up a proxy to avoid this problem. 您需要设置代理以避免此问题。 Here's the way https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development . 这是https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development的方式。 Or you can just use axios.post('http://localhost:4444/server') 或者您可以使用axios.post('http://localhost:4444/server')

Note: if you use axios.post('http://localhost:4444/server') , you will face to the cors issue. 注意: 如果您使用axios.post('http://localhost:4444/server') ,您将面临cors问题。 Use this to eliminate that issue https://www.npmjs.com/package/cors 使用它来消除该问题https://www.npmjs.com/package/cors

这通常是通过将urlencoded扩展名设置为true引起的,请尝试以下操作:

app.use(bodyParser.urlencoded({ extended: false }))

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

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