Axios 是一個基於 promise 的 HTTP 庫,可以用在瀏覽器和 node.js 中。
Features
- 從瀏覽器中創建 XMLHttpRequests
- 從 node.js 創建 http 請求
- 支持 Promise API
- 攔截請求和響應
- 轉換請求數據和響應數據
- 取消請求
- 自動轉換 JSON 數據
- 客戶端支持防御 XSRF

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>axios 實例</title>
<style type="text/css">
</style>
</head>
<body>
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<script type="text/javascript">
//get請求第一種寫法
axios({
method: 'get',
url: 'https://ipinfo.io',
params: {
ID: 12345 //參數
}
})
.then(function(response) {
console.log(response.data);
});
//get請求第二種寫法
var result = axios({
method: 'get',
url: 'https://ipinfo.io',
params: {
ID: 12345
}
});
result.then(function(response) {
console.log(response.data);
});
//post請求
axios({
method: 'post',
url: 'https://ipinfo.io',
data: {
ID: 12345 //參數
}
})
.then(function(response) {
console.log(response.data);
});
</script>
</body>
</html>
