将数据从React传递到节点快递服务器 [英] Pass Data from React to node express server

查看:60
本文介绍了将数据从React传递到节点快递服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将通用详细信息数据从我的反应前端传递到我的节点服务器。我目前收到连接被拒绝的错误。

I am trying to pass the generalDetail data from my react front end to my node server. I am currently getting a connection refused error.

(选项 http:// localhost:5000 / api / home net :: ERR_CONNECTION_REFUSED)。

(OPTIONS http://localhost:5000/api/home net::ERR_CONNECTION_REFUSED).

这是我的onSubmitForm函数:

here is my onSubmitForm function:

  onSubmitForm(e){
  e.preventDefault();

  let data = {
            generalDetail: this.state.generalDetails,
            firstName: this.state.firstName,
            middleName: this.state.middleName,
            lastName: this.state.lastName
      };

      axios.post("http://localhost:5000/home", data).then(() => {

       }).catch(() => {
          console.log("Something went wrong. Plase try again later");
      });


}
  //end

  onContentChange(fieldname, data){

    console.log('On Content Change', data);

     this.setState({
       [fieldname]: data

    });
  }



}

这是我的server.js文件

Here is my server.js file

const express = require('express');

const app = express();


// http://localhost:5000/api/home

app.post('/api/home', (req, res) => {
  const data = [
    {req.body.generalDetail},
  ];

  res.json(data);
});

const port = 5000;

app.listen(port, () => `Server running on port ${port}`);


推荐答案

尝试一下

const express = require('express')
const app = express()
const port = 8000 //your port number

const cors = require('cors')
app.use(cors())
var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); 

这篇关于将数据从React传递到节点快递服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆