![](/img/trans.png)
[英]undefined is not a function Error: $('#dialog').dialog({?
[英]Function Undefined in Axios promise
我在asp.net mvc應用程序中使用ReactJs和Axios時遇到以下問題
var ForgotPasswordForm = React.createClass({
baseUrl: "http://localhost:28804/",
getInitialState : function(){
return{isDisabled:false, text: 'Email Password Reset Link', errorText:''}
},
changeText : function(newText){
this.setState({text:newText})
},
resetText : function(){
this.setState({text: "Reset Password"})
},
setErrorText : function(error)
{
this.setState({errorText:error})
},
enableButton: function(){
this.setState({isDisabled:true});
},
disableButton: function(){
this.setState({isDisabled:false});
},
handleSubmit: function(e){
e.preventDefault();
var email = this.refs.Email.getDOMNode().value.trim();
if(!email)
{ //TODO Show some sort of error message
this.resetText();
return;
}
var d = new FormData();
d.append('Email', email);
this.changeText('Please wait...');
this.disableButton();
console.log(d);
var instance = axios.create({
baseURL: this.baseUrl
});
instance.post('/Account/Login', d)
.then(function(response){
console.log(response);
//I do not have a problem here
}).catch(function(response){
//this is where i always have an issue
console.log(response);
this.enableButton();
this.resetText();
});
return;
},
render: function(){
return (
<form className="form-horizontal" role="form" onSubmit={this.handleSubmit}>
//Omitted for brevity
</form>
);
}
});
ReactDOM.render(<ForgotPasswordForm/>, document.getElementById('form-fgt-pwd'));
問題是當請求失敗時(故意500內部服務器錯誤),我在瀏覽器控制台中獲得以下輸出
TypeError: this.enableButton is not a function
請問我做錯了什么
提前致謝
在.catch
函數中this
指的是全局范圍(在瀏覽器中它是window
),其中沒有方法enableButton
, resetText
,這些方法是ForgotPasswordForm
對象的一部分,你需要設置this
用於catch
回調,你可以用.bind
方法做到這一點
.catch(function(response){
this.enableButton();
this.resetText();
}.bind(this)); // set this for callback that refers to ForgotPasswordForm Object
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.