Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

node.js - not able to send the data to server using react and node js

not able to send the data to server.In console part of react it is showing data sent . but in server part of console its showing only {}. whats the problem i am facing

this is my react part

  import React from 'react';
    import axios from 'axios';
    class Createstudent extends React.Component {
      constructor(props) {
        super(props);
        this.state = {value: 'sri'};

        this.handleChange = this.handleChange.bind(this);
      this.handleSubmit = this.handleSubmit.bind(this);
      }

      handleChange(event) {
        this.setState({value: event.target.value});
      }

      handleSubmit(event) {
        alert(this.state.value);
        axios.post('http://localhost:8080/create',{value:this.state.value})
        .then(function(response){
          console.log("datasent");
        })

      }



      render() {
        return (
          <form onSubmit={this.handleSubmit}>
            <label>
              Name:
              <input type="text" value={this.state.value} onChange={this.handleChange} />
            </label>
            <input type="submit" value="Submit" />
          </form>
        );
      }
    }

    export default Createstudent;

this is my server part

      import config from './config';
import express from 'express';
const server = express();
import databaseInterface from './mongodbInterface';
import bodyParser from 'body-parser';
var urlencodedParser = bodyParser.urlencoded({extended : true});



server.set('view engine', 'ejs');
server.get('/', (req, res) => {
    res.render('index', {
        content: '...'
    })
});

server.get('/',function(req,res) {
  res.send("hello this is data");
  console.log("datasent");
})
var value;
  // databaseInterface.createStudent("srikanthgec", 11);
server.post('/create',urlencodedParser,function(req,res){
var response = {
  value : req.body.value
      //values : req.body.lastName
};
console.log(value);
  console.log( JSON.stringify(response));
  res.send(JSON.stringify(response));
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try changing your server code to something like this...

import config from './config';
import express from 'express';
const server = express();
import databaseInterface from './mongodbInterface';
import bodyParser from 'body-parser';


server.use( bodyParser.urlencoded({ extended: true }) );
server.set('view engine', 'ejs');

server.get('/', (req, res) => {
    res.render('index', {
        content: '...'
    })
});

server.get('/',function(req,res) {
    res.send("hello this is data");
    console.log("datasent");
})

var value; // why?
// databaseInterface.createStudent("srikanthgec", 11);
server.post('/create', function(req,res){
    console.log('req.body',req.body); // remove it once you see the correct data.
    var response = {
        value : req.body.value
      //values : req.body.lastName
    };
    console.log(value);
    console.log( JSON.stringify(response));
    res.send(JSON.stringify(response));
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...