axios的相容性處理

來源:互聯網
上載者:User

標籤:XML   post   rac   .com   cti   基於   patch   head   gif   

一、簡介

看看官網的簡介:

“Promise based HTTP client for the browser and node.js” 

譯:基於 Promise 的 HTTP 要求用戶端,可同時在瀏覽器和 node.js 中使用。

二、特點:

1、在瀏覽器中發送 XMLHttpRequests 請求;
2、在 node.js 中發送 http請求;
3、支援 Promise API;
4、攔截請求和響應;
5、轉換請求和響應資料;
6、自動轉換 JSON 資料;
7、用戶端支援保護安全免受 XSRF 攻擊;

三、安裝(官網)

四、應用

1、發送一個get請求

axios.get(‘/welfare‘, {  params: {  giftPackId: 1  } }) .then(function(res) {  console.log(res); }) .catch(function (res) {  console.log(res); });

  

2、發送一個post請求

axios.post(‘/welfare‘, {     giftPackId: 1  })  .then(function (res) {    console.log(res);  })  .catch(function (res) {    console.log(res);  });

  

  

3、發送多個並發請求

function getUserAccount() {  return axios.get(‘/welfare‘);} function getUserPermissions() {  return axios.get(‘/getWelfare‘);} axios.all([getUserAccount(), getUserPermissions()])  .then(axios.spread(function (acct, perms) {    // ok  }));

  

4、除此之外axios還提供還有如下幾種請求方式:

axios.request(config) axios.get(url[, config]) axios.delete(url[, config]) axios.head(url[, config]) axios.post(url[, data[, config]]) axios.put(url[, data[, config]]) axios.patch(url[, data[, config]])

  

 

5、相容性處理

項目中發現,在安卓4.3及以下的手機不支援axios的使用,主要就是無法使用promise。加上以下polyfill就可以了。

項目中安裝es6-promise

cnpm install es6-promise --save-dev

  

在axios.min.js開頭加上

require(‘es6-promise‘).polyfill();

  

ok! 

 

原文地址:https://www.cnblogs.com/leaf930814/p/6807318.html

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.