# Axios
Axios\[艾克絲伊歐姿\] 是一個基于 promise 的 HTTP 庫,可以用在瀏覽器和 Node.js 中。
其有以下特性:
* 在 瀏覽器中創建 XMLHttpRequests
* 在 Node.js 創建 http 請求
* 支持 Promise API
* 攔截請求和響應
* 轉換請求數據和響應數據
* 取消請求
* 自動轉換 JSON 數據
* 客戶端支持防御 XSRF
文檔,參見\[[http://www.hmoore.net/yunye/axios/234845](http://www.hmoore.net/yunye/axios/234845) \]。
# 安裝入門使用
后端使用
~~~
npm install axios
~~~
前端使用
~~~
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
~~~
# 發送 GET 請求
~~~
axios.get('/users/12345')
? .then(function (response) {
? ? ? ?console.log(response);
? ? ? ?console.log(response.data);
? ? ? ?console.log(response.status);
? ? ? ?console.log(response.statusText);
? ? ? ?console.log(response.headers);
? ? ? ?console.log(response.config);
? })
? .catch(function (error) {
? ? ? ?console.log(error);
? });
~~~
# 發送 POST 請求
~~~
axios.post('/users', {
? ? ? ?username: 'Fred'
? })
? .then(function (response) {
? ? ? ?console.log(response);
? })
? .catch(function (error) {
? ? ? ?console.log(error);
? });
~~~
# 發送 PUT 請求
~~~
axios.put('/users/12345')
? .then(function (response) {
? ? ? ?console.log(response);
? })
? .catch(function (error) {
? ? ? ?console.log(error);
? });
~~~
# 發送 DELETE 請求
~~~
axios.delete('/users/12345')
? .then(function (response) {
? ? ? ?console.log(response);
? })
? .catch(function (error) {
? ? ? ?console.log(error);
? });
~~~
實現短信發送