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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…