我有一個github配置文件查看器反應應用程序,它允許您通過用戶名搜索配置文件。在默認狀態下,我提供了自己的github用戶名,以便登錄頁面是我自己的github配置文件。您可以通過在搜索欄中輸入名稱來搜索配置文件,但是當我嘗試搜索用戶時,我的ajax
調用不工作,但是同一個函數正在爲我自己的用戶名返回有效數據。我正的錯誤是如下: -Ajax調用不適用於搜索查詢
Uncaught TypeError: $.ajax is not a function
我主要App.jsx類是如下
class App extends Component{
constructor(props){
super(props);
this.state = {
username: 'kinny94',
userData: [],
userRepos: [],
perPage: 5
}
}
getUserData(){
$.ajax({
url: 'https://api.github.com/users/' + this.state.username + '?client_id=' + this.props.clientId + '&client_secret=' + this.props.clientSecret,
dataType: 'json',
cache: false,
success: function(data){
this.setState({
userData: data
});
console.log(data);
}.bind(this),
error: function(xhr, status, err){
this.setState({
username: null
});
alert(err);
}.bind(this)
});
}
getUserRepos(){
$.ajax({
url: 'https://api.github.com/users/' + this.state.username + '/repos?per_page='+ this.state.perPage +'client_id=' + this.props.clientId + '&client_secret=' + this.props.clientSecret + '&sort=created',
dataType: 'json',
cache: false,
success: function(data){
this.setState({
userRepos: data
});
}.bind(this),
error: function(xhr, status, err){
this.setState({
username: null
});
alert(err);
}.bind(this)
});
}
handleformSubmit(username){
this.setState({
username: username
}, function(){
this.getUserData();
this.getUserRepos();
})
}
componentDidMount(){
this.getUserData();
this.getUserRepos();
}
render(){
return(
<div>
<Search onFormSubmit={this.handleformSubmit.bind(this)}/>
<Profile {...this.state}/>
</div>
)
}
}
export default App;
和我的搜索文件如下
class Search extends Component{
onSubmit(e){
e.preventDefault();
let username = this.refs.username.value.trim();
if(!username){
alert('Please Enter a username!');
return;
}
this.props.onFormSubmit(username);
this.refs.username.value = '';
}
render(){
return(
<div>
<form onSubmit={this.onSubmit.bind(this)}>
<label>Search Github Users </label>
<input type="text" ref="username" className="form-control" />
</form>
</div>
)
}
}
我的問題是,如果相同的ajax
呼叫爲我的用戶名工作,爲什麼它不工作的一個新的隨機用戶名。
只是一個建議。您正在加載JQUERY只需通過ajax進行api調用,如果您的唯一目的是進行api調用,則可以使用其他輕量級庫(如axios或superagent)或提取api。 –