Javascript 如何在reactjs中设置responseajax请求的状态
我绑定并使用这个仍然得到这个错误,做了同样的事情 另一个,当我在这里做同样的事情时,它工作得很好,但是我得到了一个错误Javascript 如何在reactjs中设置responseajax请求的状态,javascript,ajax,reactjs,Javascript,Ajax,Reactjs,我绑定并使用这个仍然得到这个错误,做了同样的事情 另一个,当我在这里做同样的事情时,它工作得很好,但是我得到了一个错误 class Player extends React.Component{ constructor(){ super(); this.setState={ video:{} }; } componentDidMount () { var sess=localSt
class Player extends React.Component{
constructor(){
super();
this.setState={
video:{}
};
}
componentDidMount () {
var sess=localStorage.getItem('sessionId');
var id=this.props.params.vid;
本地ajax请求控制台日志显示我的请求是好的
您可以使用React生命周期方法发出请求 实例
您可以使用React生命周期方法发出请求 实例
您需要从以下位置修改构造函数方法:
constructor(){
super();
this.setState={
video:{}
};
}
致:
另外,我认为
this.setState()
调用中的this
并没有引用$.ajax()调用中的类。尝试绑定两个函数:ajax()
调用和success()
函数,或者对success()
方法使用箭头函数。您需要从以下位置修改构造函数方法:
constructor(){
super();
this.setState={
video:{}
};
}
致:
另外,我认为this.setState()
调用中的this
并没有引用$.ajax()调用中的类。尝试绑定两个函数:ajax()
调用和success()
函数,或者对success()
方法使用箭头函数。在构造函数中,只需执行this.state={video:{}
而不是this.setState
constructor(){
super();
this.state = {
video:{}
};
}
this.setState
可以在构造函数之外的任何地方使用。非es6的做法是:
getInitialState: function() {
return {
video: {}
}
}
这相当于在构造函数中执行this.state={}
在构造函数中,您只需要执行this.state={video:{}
而不是this.setState
constructor(){
super();
this.state = {
video:{}
};
}
this.setState
可以在构造函数之外的任何地方使用。非es6的做法是:
getInitialState: function() {
return {
video: {}
}
}
这相当于只在构造函数中执行this.state={}
以下是我如何使其工作的
class Login extends Component{
constructor(props){
super(props);
this.state={
email:'',
password:''
emailError:'',
passwordError:''
}
this.userLogin=this.userLogin.bind(this);
this.handleUserInput=this.handleUserInput.bind(this);
}
handleUserInput(){
const name = e.target.name;
const value = e.target.value;
this.setState({[name]: value})
}
userLogin(){
var body={
email:this.state.email,
password:this.state.password
}
$.ajax({
type: 'POST',
url: 'http://localhost:8080/userLogin',
data: body,
success:function(data){
console.log(data);
if(data==='valid'){
window.location.href='/dashboard';
}
else{
//setting error details here
this.setState({emailError:'email doesn\'t exist',
passwordError:'incorrect password'});
window.location.href='/';
}
}.bind(this),
error:function(){
console.log("error");
this.setState({emailError:'email doesn\'t exist',
passwordError:'incorrect password'});
}.bind(this)
});
}
render(){
return(
<div>
<h2>Login:</h2>
<form>
<input type="text" placeholder="email" name="email" onChange={(event)
=> this,handleUserInput(event)}/>
<h6>{this.state.emailError}</h6>
<input type="text" placeholder="email" name="email" onChange={(event)
=> this,handleUserInput(event)}/>
<h6>{this.state.passwordError}</h6>
<button onClick={this.userlogin}>Login</button>
</form>
</div>
)
}
}
类登录扩展组件{
建造师(道具){
超级(道具);
这个州={
电子邮件:“”,
密码:“”
电子邮件错误:“”,
密码错误:“”
}
this.userLogin=this.userLogin.bind(this);
this.handleUserInput=this.handleUserInput.bind(this);
}
handleUserInput(){
const name=e.target.name;
常量值=e.target.value;
this.setState({[name]:value})
}
userLogin(){
变位体={
电子邮件:this.state.email,
密码:this.state.password
}
$.ajax({
键入:“POST”,
网址:'http://localhost:8080/userLogin',
资料来源:body,
成功:功能(数据){
控制台日志(数据);
如果(数据=='valid'){
window.location.href='/dashboard';
}
否则{
//在此处设置错误详细信息
this.setState({emailError:'email不存在',
密码错误:'密码不正确'});
window.location.href='/';
}
}.绑定(此),
错误:函数(){
控制台日志(“错误”);
this.setState({emailError:'email不存在',
密码错误:'密码不正确'});
}.绑定(此)
});
}
render(){
返回(
登录:
此,handleUserInput(事件)}/>
{this.state.emailError}
此,handleUserInput(事件)}/>
{this.state.passwordError}
登录
)
}
}
以下是我如何让它工作的
class Login extends Component{
constructor(props){
super(props);
this.state={
email:'',
password:''
emailError:'',
passwordError:''
}
this.userLogin=this.userLogin.bind(this);
this.handleUserInput=this.handleUserInput.bind(this);
}
handleUserInput(){
const name = e.target.name;
const value = e.target.value;
this.setState({[name]: value})
}
userLogin(){
var body={
email:this.state.email,
password:this.state.password
}
$.ajax({
type: 'POST',
url: 'http://localhost:8080/userLogin',
data: body,
success:function(data){
console.log(data);
if(data==='valid'){
window.location.href='/dashboard';
}
else{
//setting error details here
this.setState({emailError:'email doesn\'t exist',
passwordError:'incorrect password'});
window.location.href='/';
}
}.bind(this),
error:function(){
console.log("error");
this.setState({emailError:'email doesn\'t exist',
passwordError:'incorrect password'});
}.bind(this)
});
}
render(){
return(
<div>
<h2>Login:</h2>
<form>
<input type="text" placeholder="email" name="email" onChange={(event)
=> this,handleUserInput(event)}/>
<h6>{this.state.emailError}</h6>
<input type="text" placeholder="email" name="email" onChange={(event)
=> this,handleUserInput(event)}/>
<h6>{this.state.passwordError}</h6>
<button onClick={this.userlogin}>Login</button>
</form>
</div>
)
}
}
类登录扩展组件{
建造师(道具){
超级(道具);
这个州={
电子邮件:“”,
密码:“”
电子邮件错误:“”,
密码错误:“”
}
this.userLogin=this.userLogin.bind(this);
this.handleUserInput=this.handleUserInput.bind(this);
}
handleUserInput(){
const name=e.target.name;
常量值=e.target.value;
this.setState({[name]:value})
}
userLogin(){
变位体={
电子邮件:this.state.email,
密码:this.state.password
}
$.ajax({
键入:“POST”,
网址:'http://localhost:8080/userLogin',
资料来源:body,
成功:功能(数据){
控制台日志(数据);
如果(数据=='valid'){
window.location.href='/dashboard';
}
否则{
//在此处设置错误详细信息
this.setState({emailError:'email不存在',
密码错误:'密码不正确'});
window.location.href='/';
}
}.绑定(此),
错误:函数(){
控制台日志(“错误”);
this.setState({emailError:'email不存在',
密码错误:'密码不正确'});
}.绑定(此)
});
}
render(){
返回(
登录:
此,handleUserInput(事件)}/>
{this.state.emailError}
此,handleUserInput(事件)}/>
{this.state.passwordError}
登录
)
}
}
您应该在生命周期方法componentDidMount
中处理此问题。我仍然收到与您设置的this.setState={video:{}相同的错误
这就是问题所在。那么我应该做什么呢?你应该在生命周期方法componentDidMount
中处理这个问题。我仍然会遇到你设置的相同错误this.setState={video:{}}
这就是问题所在。那么我应该如何用你的确切类代码编辑你的问题呢,包括导入和标题。使用success
回调的语法,如上面的示例所示。使用准确的类代码编辑您的问题,包括导入和标题。使用success
回调的语法,如上面的示例所示。您是否仍然收到setState
不是函数的错误消息?是否仍然收到setState
不是函数的错误消息?