我曾嘗試使用ajax調用來從軌道獲取數據作出反應,但狀態沒有更新。當我試圖調用狀態時,它給了我一個未定義的錯誤。我在堆棧溢出中提到了其他問題,但無法解決我的問題。這是我的阿賈克斯請求。如何從軌道獲取json數據來反應js?
fetchData: function(){
$.ajax({
url: '/orders',
dataType: 'json',
type: 'GET',
success: function(result) {
this.setState({orders: result});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
}
Click this link to view the error
<p><button onClick={this.fetchData}>Filter</button></p>
<div>
{
Object.keys(this.state.orders).map(function(key){
return <div key={ key }>{this.state.orders[key]}</div>
}, this)
}
</div>
請提供一些更多的細節你的反應組件和錯誤,你得到 – WitVault
未定義的錯誤可能意味着空或不正確的json返回 –