# axios ###### tags: `axios` `ES6` ## 使用 ``` axios .get('/api',{ paramas:{ key: value} }){ }.then(response){ }.catch(error){ //status code is not 200 } ``` ## 抓取裡面的資料 then response資料裡面有 ``` response.data //回傳的response資料 response.config response.status response.statusText response.request response.headers ``` catch error的資料 ``` error.message //回傳錯誤訊息 error.response.status //回傳響應代碼 error.response.data //回傳response資料 ``` ## 設定headers的三種方式 ``` let config = { headers: { header1: value, } } let data = { 'HTTP_CONTENT_LANGUAGE': self.language } axios.post(URL, data, config).then(...) or axios.get('https://example.com/getSomething', { headers: { Authorization: 'Bearer ' + token //the token is a variable which holds the token } }) or axios.defaults.headers.post['header1'] = 'value' // for POST requests axios.defaults.headers.common['header1'] = 'value' // for all requests ``` ## 搭配 Promise.all 用法 ``` function getSBData() { return axios.get("/cgi-bin/Get.cgi?K=SENSOR_BINARY&out=json"); } function getSWData() { return axios.get("/cgi-bin/Get.cgi?K=SOFTWARE&out=json"); } function getSMData() { return axios.get("/cgi-bin/Get.cgi?K=SENSOR_MULTILEVEL&out=json"); } //axios全部請求完成後,才會執行then這個方法 Promise.all([getSBData(), getSWData(), getSMData()]) .then(([SB_data, SW_data, SM_data]) => { //SB_data.data //SW_data.data }).catch(error => { }); ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up