vue中axios處理http發送請求的示例(Post和get)

axios中文文檔:https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-formatjavascript

一、安裝

node方式vue

npm install axios

設置index.js:java

import axios from 'axios'
Vue.prototype.$ajax = axios

 

或者使用cdn方式node

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

2.get請求

// 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);
 });

3.Post請求

axios.post('/user', {
 firstName: 'Fred',
 lastName: 'Flintstone'
})
.then(function (response) {
 console.log(response);
})
.catch(function (error) {
 console.log(error);
});

4.執行多個請求

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
 }));

5.使用 application/x-www-urlencoded 形式的post請求:

var qs = require('qs');
 axios.post('/bbg/goods/get_goods_list_wechat', qs.stringify({"data": JSON.stringify({
  "isSingle": 1,
  "sbid": 13729792,
  "catalog3": 45908012,
  "offset": 0,
  "pageSize": 25
 })}), {
  headers: {
   "BBG-Key": "ab9ef204-3253-49d4-b229-3cc2383480a6",
  }
 })
 .then(function (response) {
  // if (response.data.code == 626) {
   console.log(response);
  // }
 }).catch(function (error) {
  console.log(error);
 });

6.注意:

 對於post請求,通常狀況下,第一個參數是url,第二個參數是要發送的請求體的數據,第三個參數是對請求的配置。jquery

另外:axios默認是application/json格式的,若是不適用 qs.stringify 這種形式, 即便添加了請求頭 最後的content-type的形式仍是 json 的。ios

7.對於post請求,咱們也可使用下面的jquery的ajax來實現:

$.ajax({
 url:'api/bbg/goods/get_goods_list_wechat',
 data:{
  'data': JSON.stringify({
        "isSingle": 1,
        "sbid": 13729792,
        "catalog3": 45908012,
        "offset": 0,
        "pageSize": 25
      })    
 },  
 beforeSend: function(request) {
  request.setRequestHeader("BBG-Key", "ab9ef204-3253-49d4-b229-3cc2383480a6");
 }, 
 type:'post', 
 dataType:'json', 
 success:function(data){   
  console.log(data);
 },
 error: function (error) {
  console.log(err);
 },
 complete: function () {
 
 }
});

顯然,經過比較,能夠發現,jquery的請求形式更簡單一些,且jqury默認的數據格式就是 application/x-www-urlencoded ,從這方面來說會更加方便一些。git

另外,對於兩個一樣的請求,即便都請求成功了,可是二者請求獲得的結果也是不同的,以下:github

不難看到: 使用axios返回的結果會比jquery的ajax返回的結構(實際的結果)多包裝了一層,包括相關的config、 headers、request等。ajax

對於get請求, 我我的仍是推薦使用axios.get()的形式,以下所示:npm

axios.get('/bbg/shop/get_classify', {
 params: {
  sid: 13729792
 },
 headers: {
  "BBG-Key": "ab9ef204-3253-49d4-b229-3cc2383480a6"
 }
})
.then(function (response) {
 if (response.data.code == 130) {
  items = response.data.data;
  store.commit('update', items);
  console.log(items);
 }
 console.log(response.data.code);
}).catch(function (error) {
 console.log(error);
 console.log(this);
});

 

參考文檔:

1.vue中axios處理http發送請求的示例(Post和get):http://www.jb51.net/article/125717.htm

相關文章
相關標籤/搜索