我對ReactJS相當陌生,並且已經學習了不到2個月,我試圖重新創建一個在線看到的項目,並將其更新到ES6 https://github.com/perborgen/YourSecondReactProject/blob/master/finished_project.html。ES6上的Ajax調用;獲取未定義錯誤
問題是,我從來沒有嘗試過之前做過任何調用,比如Ajax,抓取等。所以當我開始這個時,我得到一個錯誤,我的ajax是未定義的,我很漂亮我確信我的語法或格式可能有誤,請幫助我解釋我的錯誤以及解決方法。非常感謝!
P.S.我沒有iTunes帳戶,所以我使用了Tmdb API。
這裏就是我試圖做
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
class App extends Component {
state = {searchResults:[]};
showResults(response){
this.setState({searchResults: response.results})
}
search(URL){
ajax({
type: "GET",
dataType: 'jsonp',
url: URL,
success: function(response){
this.showResults(response);
}.bind(this)
});
}
render(){
return(
<div>
<SearchBox search={this.search}/>
<Results searchResults={this.state.searchResults}/>
</div>
);
}
}
class SearchBox extends Component{
render(){
return(
<div>
<input type="text" ref="query" placeholder="Search Movies
Here"/>
<input type="submit" onClick={this.makeAjax}/>
</div>
);
}
makeAjax(){
var query = ReactDOM.findDOMNode(this.refs.query).value;
var URL = 'https://api.themoviedb.org/3/search/movie?
api_key=0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&language=en-
US&query='+query+'&page=1&include_adult=false';
this.props.search(URL)
}
}
class Results extends Component{
render(){
var resultItems = this.props.searchResults.map((result) => {
return(
<ResultItem key = {result.id} title={result.title}/>)
});
return(
<ul>
{resultItems}
</ul>
);
}
}
class ResultItem extends Component{
render(){
return <li>{this.props.trackName}</li>;
}
}
我建議從這裏開始:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest –