0
我想通過ajax獲取json,如果我的響應變量是可以接受的使用react路由器重定向。我怎樣才能做到這一點?ReactJs重定向狀態變量
successRedirect(){
if (this.responseCode.equals("what I need")) {
router.transitionTo('/')
}
}
createCheckout() {
$.ajax({
url: "someurl",
dataType: 'json',
type: 'POST',
cache: false,
data: this.data,
success: function(response) {
this.setState({
response: response,
responseCode: response.result.code
});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
})
}
功能必須在做出響應後調用。例如,我有這樣的代碼,並在呈現響應取並在一段時間後顯示:
render(){
return (
<div>
<div>Response - {this.state.responseCode}</div>
</div>
);
}
我不明白這一點..所以調用'success'功能? – azium
我得到了undefind this.responseCode – TeodorKolev
你是不是指'this.state.responseCode'? – azium