我正在使用axios發出ajax請求。我是一個新手提出ajax請求,並不理解爲什麼這段代碼不工作?我刪除了一些與這個問題無關的代碼,爲什麼它不工作。錯誤顯示爲「GET https://api.github.com/users/ $%7Bthis.state.usersName%7D 404(Not Found)」。也許我在寫請求錯誤的URL部分...使用axios反應ajax請求
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import axios from 'axios';
const Card = (props) => {
return(
<div style={{margin: '1em'}}>
<img src={props.Avatar_URL} width="120px"/>
<div>
<div>{props.Name}</div>
<div>{props.Company}</div>
</div>
</div>
);
};
const CardList= (props) => {
return(
<div>
{props.Card.map(card => <Card {...card}/>)}
</div>
);
};
class MyForm extends React.Component{
state = { userName: '' }
handlesSubmit = (event) => {
event.preventDefault();
console.log('Event: Form Submitted:', this.state.userName);
axios.get("api.github.com/users/" + this.state.userName).then(response => {
console.log(response);
})
};
render(){
return(
<form onSubmit={this.handlesSubmit}>
<div style={{margin: '1em'}}>
<input type="text" placeholder="@Github"
value={this.state.userName}
onChange={(event) => this.setState({ userName: event.target.value})}/>
<button type="submit">Search</button>
</div>
</form>
);
};
};
class MyApp extends React.Component{
render(){
return(
<div>
<MyForm />
<CardList Card={data} />
</div>
);
};
};
let data = [
{
Avatar_URL: "https://avatars2.githubusercontent.com/u/35?v=4",
Name: "Kevin Williams",
Company: "Oracle"
},
{
Avatar_URL: "https://avatars2.githubusercontent.com/u/36?v=4",
Name: "Dave Fayram",
Company: "Udacity, Inc"
}
]
ReactDOM.render(
<MyApp />,
document.getElementById('root')
);
404是一個「找不到網頁錯誤」,因此檢查網址 – AlexVestin
都能跟得上它沒有任何工作..我什至嘗試把它放入一個變量,並沒有工作 –
你正在使用usersName而獲取配置文件,但用戶名在 – AlexVestin