axios學習筆記

來源:互聯網
上載者:User

標籤:orm   first   rip   概念   bsp   ase   mission   筆記   miss   

axios學習筆記
axios文檔源地址:https://github.com/axios/axios
0.概念
axios 在NPM上的描述是:Promise based HTTP client for the browser and node.js。
axios是通過promise實現對ajax技術的一種封裝,就像jQuery實現ajax封裝一樣。
簡單來說: ajax技術實現了網頁的局部資料重新整理,axios實現了對ajax的封裝。
1.Installing
$ npm install axios
2.Example
1.Performing a GET request
// Make a request for a user with a given ID
axios.get(‘/user?ID=12345‘)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
// Optionally the request above could also be done as
axios.get(‘/user‘, {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

// 非同步作業 Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
try {
const response = await axios.get(‘/user?ID=12345‘);
console.log(response);
} catch (error) {
console.error(error);
}
}
NOTE: async/await is part of ECMAScript 2017 and is not supported in Internet Explorer and older browsers, so use with caution.
2.Performing a POST request
axios.post(‘/user‘, {
firstName: ‘Fred‘,
lastName: ‘Flintstone‘
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
3.多種並發請求
function getUserAccount() {
return axios.get(‘/user/12345‘);
}

function getUserPermissions() {
return axios.get(‘/user/12345/permissions‘);
}

axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// Both requests are now complete
}));
3.axios API(和ajax很像)
1.post
axios({
method: ‘post‘,
url: ‘/user/12345‘,
data: {
firstName: ‘Fred‘,
lastName: ‘Flintstone‘
}
});
2.get
axios({
method:‘get‘,
url:‘http://bit.ly/2mTM3nY‘,
responseType:‘stream‘
})
.then(function(response) {
response.data.pipe(fs.createWriteStream(‘ada_lovelace.jpg‘))
});
3.預設get方法
axios(‘/user/12345‘);
4.requireJs
4.1作用
防止JS阻塞瀏覽器渲染
4.2使用
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.16/require.min.js"></script>

 

axios學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.