axios中文文档 vue
https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format axios文档node
在处理http请求方面,已经不推荐使用vue-resource了,而是使用最新的axios,下面作一个简单的介绍。jquery
使用nodeios
npm install axios
使用cdngit
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
// 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); });
axios.post('/user', { firstName: 'Fred', lastName: 'Flintstone' }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
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 }));
这个的使用方法其实和原生的ajax是同样的,一看就懂。github
使用 application/x-www-urlencoded 形式的post请求:ajax
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); });
具体使用参考文档: https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-formatnpm
注意: 对于post请求,通常状况下,第一个参数是url,第二个参数是要发送的请求体的数据,第三个参数是对请求的配置。json
另外:axios默认是application/json格式的,若是不适用 qs.stringify 这种形式, 即便添加了请求头 最后的content-type的形式仍是 json 的。 axios
对于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 ,从这方面来说会更加方便一些。
另外,对于两个一样的请求,即便都请求成功了,可是二者请求获得的结果也是不同的,以下:
不难看到: 使用axios返回的结果会比jquery的ajax返回的结构(实际的结果)多包装了一层,包括相关的config、 headers、request等。
对于get请求, 我我的仍是推荐使用axios.get()的形式,以下所示:
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); });
即第一个参数是:url, 第二个参数就是一个配置对象,咱们能够在配置对象中设置 params 来传递参数。
我的理解为何get没有第二个参数做为传递的查询字符串,而post有第二个参数做为post的数据。
由于get能够没有查询字符串,也能够get请求,可是post必需要有post的数据,要否则就没有使用post的必要了。