gpt4 book ai didi

javascript - 使用 Axios post 响应表单

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:59:44 24 4
gpt4 key购买 nike

我已经在互联网上搜索了好几天,但似乎找不到任何与通过 React 提交表单请求和使用 Axios post 将信息输入到我们的 REST API 相关的内容。每次我们提交注册表单时,每个值都未定义。这是让 React 与我们的 REST API 通信的最佳方式吗?我们还使用 Postman 测试了 API,并且知道它可以正常工作。

var React = require('react');

var Access = React.createClass({
getInitialState: function() {
return {
firstName: '',
lastName: '',
email: '',
password1: ''
}
},

handleSubmit: function(e) {
var _this = this;
this.serverRequest = axios
console.log(_this.ref.firstName)
.post("/api/Users", {
userFirstName: _this.ref.firstName,
userLastName: _this.ref.lastName,
userEmail: _this.ref.email,
userPassword: _this.ref.password1
})
.then(function(response) {
console.log(response);
}) .catch(function (error) {
console.log(error);
});
},

render: function() {
return(
<div>
<section className="access">
<div className="row center-xs container">
<div className="col-xs-12 col-sm-4 sign-in">
<h1>Sign-In</h1>
<form action="/" method="get">
<label htmlFor="email">Email</label>
<input type="email" name="email" placeholder="Email"/>
<label htmlFor="password">Password</label>
<input type="password" name="password" placeholder="Password"/>
<input className="button pink" type="submit" value="Sign-In"/>
<br/>
<input type="checkbox" name="RememberMe"/>
<label htmlFor="RememberMe">Remember me</label>
<span> | <a href="/">Forgot password?</a></span>
</form>
</div>
<div className="col-xs-12 col-sm-4 register">
<h1>Register</h1>
<form onSubmit={this.onSubmit}>
<label htmlFor="firstName">First Name</label>
<input type="text" name="firstName" placeholder="First Name" ref="firstName"/>
<label htmlFor="lastName">Last Name</label>
<input type="text" name="lastName" placeholder="Last Name" ref="lastName"/>
<label htmlFor="email">Email</label>
<input type="email" name="email" placeholder="Email" ref="email"/>
<label htmlFor="password1">Password</label>
<input type="password" name="password1" placeholder="Password" ref="password1"/>
<label htmlFor="password2">Re-enter Password</label>
<input type="password" name="password2" placeholder="Password"/>
<input className="button gold" type="submit" value="Register"/>
</form>
</div>
</div>
</section>
</div>
);
}
});

module.exports = Access;

最佳答案

发生这种情况是因为您使用 _this.ref.firstName 而不是 _this.refs.firstName 访问 refs

此外,您不应再使用字符串引用。而是遵循 react 文档推荐的回调方法

var Access = React.createClass({
getInitialState: function() {
return {
firstName: '',
lastName: '',
email: '',
password1: ''
}
},

handleSubmit: function(e) {
var _this = this;

console.log(_this.firstName);
this.serverRequest = axios
.post("/api/Users", {
userFirstName: _this.firstName,
userLastName: _this.lastName,
userEmail: _this.email,
userPassword: _this.password1
})
.then(function(response) {
console.log(response);
}) .catch(function (error) {
console.log(error);
});
},

render: function() {
return(
<div>
<section className="access">
<div className="row center-xs container">
<div className="col-xs-12 col-sm-4 sign-in">
<h1>Sign-In</h1>
<form action="/" method="get">
<label htmlFor="email">Email</label>
<input type="email" name="email" placeholder="Email"/>
<label htmlFor="password">Password</label>
<input type="password" name="password" placeholder="Password"/>
<input className="button pink" type="submit" value="Sign-In"/>
<br/>
<input type="checkbox" name="RememberMe"/>
<label htmlFor="RememberMe">Remember me</label>
<span> | <a href="/">Forgot password?</a></span>
</form>
</div>
<div className="col-xs-12 col-sm-4 register">
<h1>Register</h1>
<form onSubmit={this.onSubmit}>
<label htmlFor="firstName">First Name</label>
<input type="text" name="firstName" placeholder="First Name" ref={firstName => this.firstName = firstName}/>
<label htmlFor="lastName">Last Name</label>
<input type="text" name="lastName" placeholder="Last Name" ref={lastName => this.lastName = lastName}/>
<label htmlFor="email">Email</label>
<input type="email" name="email" placeholder="Email" ref={email => this.email = email}/>
<label htmlFor="password1">Password</label>
<input type="password" name="password1" placeholder="Password" ref={password1 => this.password1 = password1}/>
<label htmlFor="password2">Re-enter Password</label>
<input type="password" name="password2" placeholder="Password"/>
<input className="button gold" type="submit" value="Register"/>
</form>
</div>
</div>
</section>
</div>
);
}
});

关于javascript - 使用 Axios post 响应表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43251394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com