【发布时间】:2019-01-04 09:17:29
【问题描述】:
在反应应用程序中,我想使用 axios/post 方法将输入字段值提交到数据库,因为我是反应新手,所以我没有任何明确的想法。 谁能帮我举个例子?提前致谢
我尝试了以下程序
import React, { Component } from 'react';
import axios from 'axios';
var panelStyle = {
'max-width': '80%',
margin: '0 auto'
}
class Register extends Component {
constructor() {
super();
this.state = {
formFields: {username: ''}
}
}
render() {
return(
<div>
<div class="panel panel-primary" style={panelStyle}>
<div class="panel panel-heading">React Forum - Register</div>
<div class="panel panel-body">
<form onsubmit={this.formHandler(this.state.formFields)}>
<strong>Username:</strong> <br /> <input type="text" name="username" placeholder="Nathaniel" onChange={(e) => this.inputChangeHandler.call(this, e)} value={this.state.formFields.username} /> <br />
<strong>Email:</strong> <br /> <input type="email" name="email" placeholder="me@example.com" /> <br />
<strong>Confirm Email:</strong> <br /> <input type="email" name="confirmemail" placeholder="me@example.com" /> <br />
<strong>Password:</strong> <br /> <input type="password" name="password" placeholder="********" /> <br />
<strong>Confirm Password:</strong> <br /> <input type="password" name="confirmpassword" placeholder="********" /> <br /><br />
<button class="btn btn-primary">Register Account</button>
</form>
</div>
</div>
</div>
);
}
inputChangeHandler(e) {
let formFields = {...this.state.formFields};
formFields[e.target.name] = e.target.value;
this.setState({
formFields
});
}
formHandler(formFields) {
axios.post('/api/register', formFields)
.then(function(response){
console.log(response);
//Perform action based on response
})
.catch(function(error){
console.log(error);
//Perform action based on error
});
}
}
export default Register
并在控制台中出现以下错误
index.js:2178 Warning: The tag <children> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.
【问题讨论】:
标签: javascript reactjs mongoose postman axios