简体   繁体   English

将数据从React传递到节点快递服务器

[英]Pass Data from React to node express server

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. 我目前收到连接被拒绝的错误。

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

here is my onSubmitForm function: 这是我的onSubmitForm函数:

  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

    });
  }



}

Here is my server.js file 这是我的server.js文件

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}`);

try this 尝试这个

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
})); 

You can change your code into this 您可以将代码更改为此

Example

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/api/home", data).then(() => {
           //do something
         }).catch(() => {
            console.log("Something went wrong. Plase try again later");
        });
    }

Try to add cors preflight code in your backend code (server.js). 尝试在您的后端代码(server.js)中添加cors预检代码。

app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
  res.header("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS");
  res.header("Access-Control-Allow-Credentials", true);
  next();
});
app.post('/api/home', (req, res) => {
  const data = [{ generalDetails: req.body.generalDetail }];
  res.json(data);
});

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

相关问题 从 Node/Express 服务器在 React 中播放音频数据 - Playing audio data in React from Node/Express server Node Express将变量从客户端传递到服务器 - Node Express pass variable from client to server 如何将数据从React组件传递到节点server.js? - How to pass data from React component to node server.js? 从快递服务器传递变量以响应应用程序 - Pass variable from express server to react application 如何将文件(PDF、DOC 等)从客户端(react)传递到服务器(Node - Express) - How to pass a file (PDF, DOC, etc) from Client (react) to server (Node - Express) 如何在允许清理HTML但阻止XSS的同时将数据从Node / Express服务器传递到客户端(JavaScript)? - How to pass data from a Node/Express server to the client (JavaScript) while allowing sanitized HTML but preventing XSS? 将 Node Express 服务器上长时间运行的异步 function 的进度数据发送到反应客户端 - Send progress data from a long running async function on a Node Express Server to a react client 无法将数据从反应发送到快递服务器 - Unable to send data from react to express server 将数据从快速路由传递到节点模块 export function - Pass data from express route into node module export function 如何使用Ember-Ajax从Node JS Express传递数据? - How to pass in data from Node JS Express with Ember-Ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM