簡體   English   中英

從反應前端和節點快速后端(corse 和端點)傳遞數據的問題

[英]Issue with passing data from react front end and node express backend(corse and endpoints)

我有一個問題,當我提交表單數據時,我收到 404 錯誤,說我的端點不存在。 (OPTIONS http://localhost:8080/home 404 (Not Found) 請在這里查看我的 server.js 文件。當數據傳遞到后端時,我想從我的節點快遞服務器發送一封電子郵件.

const express = require('express');
const bodyParser = require('body-parser')
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
const cors = require('cors')

app.use(cors())

app.get('/Home', function (req, res) {
 return res.send('home');
});

app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

app.listen(process.env.PORT || 8080);

這是我的提交功能

import React, { Component } from 'react';
import './App.css';
import PageOne from './Components/PageOne';
import PageTwo from './Components/PageTwo';
import PageThree from './Components/PageThree';
import PageFour from './Components/PageFour';
import PageFive from './Components/PageFive';
import PageSix from './Components/PageSix';
import {Button,  } from 'semantic-ui-react';
import axios from 'axios';


class App extends Component {


  constructor(props){
    super(props)
    this.state={
      generalDetails: "Text",
      fName: "Text",
      mName: "Text",
      LName: "Text",
      gender: "Text",

    }

    this.onContentChange = this.onContentChange.bind(this);
    this.onSubmitForm = this.onSubmitForm.bind(this);
  }




  render() {

    return (

      <div className="App">


      <PageOne handleChange={this.onContentChange}  />
      <PageTwo handleChange={this.onContentChange} />
      <PageThree handleChange={this.onContentChange} />
      <PageFour handleChange={this.onContentChange} />
      <PageFive handleChange={this.onContentChange}/>
      <PageSix handleChange={this.onContentChange} />

      <Button onClick={this.onSubmitForm} >
      Submit Form
      </Button>

    <br/>
    <br/>
      </div>
    );
  }



  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:8080/home", data).then(() => {
      //do something
      console.log('succesful!', this.state.generalDetails);
    }).catch(() => {
       console.log("Something went wrong");
   });


  console.log(
    //start of page one
    '* general Details: ' ,this.state.generalDetails,
    '* First Name: ', this.state.fName,
    '* Middle Name: ', this.state.mName,
    '* Last Name: ', this.state.lName,


  );
}
  //end

  onContentChange(fieldname, data){

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

     this.setState({
       [fieldname]: data

    });
  }



}

你在做axios.post("http://localhost:8080/home"

但是您的快遞服務器正在尋找獲取請求。 將其更改為app.post

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM