广东外贸网站推广,网站频道建设,网易企业邮箱域名怎么设置,找人做网站要注意什么1.axios介绍## 1. vue本身不支持发送AJAX请求#xff0c;需要使用vue-resource、axios等插件实现## 2. axios是一个基于Promise的HTTP请求客户端#xff0c;用来发送请求#xff0c;也是vue2.0官方推荐的#xff0c;同时不再对vue-resource进行更新和维护## 3. 参考#x…1.axios介绍## 1. vue本身不支持发送AJAX请求需要使用vue-resource、axios等插件实现## 2. axios是一个基于Promise的HTTP请求客户端用来发送请求也是vue2.0官方推荐的同时不再对vue-resource进行更新和维护## 3. 参考GitHub上搜索axios查看API文档https://github.com/axios/axios3.axios安装## 1. npm install axios -S # 也可直接下载axios.min.js文件## 2. 下载后即到 C:\Users\tom\node_modules\axios\dist 路径下找到 axios.min.js 文件4.axios基本用法4.1axios:get的请求参数发送AJAX请求GET方式发送AJAX请求window.οnlοadfunction(){new Vue({el:#itany,data:{uid:},methods:{sendGet(){// 1、发送get请求axios({url: http://127.0.0.1:8000/data/, //1、请求地址method: get, //2、请求方法params: {ids: [1,2,3],type: admin}, //3、get请求参数})// 2、回调函数.then(resp {console.log(resp.data);})// 3、捕获异常.catch(err {console.log(请求失败err.status,err.statusText);});}}});}get: axios最基本get请求参数4.2axios post基本请求参数发送AJAX请求POST方式发送AJAX请求window.οnlοadfunction(){new Vue({el:#itany,data:{uid:},methods:{sendPost(){// 1、发送post请求axios({url: http://127.0.0.1:8000/data/, //1、请求地址method: post, // 2、请求方法data: {ids: [1,2,3],type: admin}, //3、提交数据transformRequest:[ //4、在发送请求前可以改变要传的数据function(data){let params;for(let index in data){paramsindexdata[index]; //5、拼接成namealiceage20 的字符串}return params;}]})// 2、回调函数.then(resp {console.log(resp.data);})// 3、捕获异常.catch(err {console.log(请求失败err.status,err.statusText);});}}});}post: axios发送最基本post请求参数2、axios借助Qs对提交数据进行序列化3. axios get请求参数发送AJAX请求GET方式发送AJAX请求window.οnlοadfunction(){new Vue({el:#itany,data:{uid:},methods:{sendGet(){// 1、发送get请求axios({url: http://127.0.0.1:8000/data/, //1、请求地址method: get, //2、请求方法params: {ids: [1,2,3],type: admin}, //3、get请求参数paramsSerializer: params { //4、可选函数、序列化paramsreturn Qs.stringify(params, { indices: false })},responseType: json, //5、返回默认格式jsonheaders: {authorization: xxxtokenidxxxxx}, //6、认证token})// 2、回调函数.then(resp {console.log(resp.data);})// 3、捕获异常.catch(err {console.log(请求失败err.status,err.statusText);});}}});}getaxios发送get请求5.post:axios请求参数发送AJAX请求POST方式发送AJAX请求window.οnlοadfunction(){new Vue({el:#itany,data:{uid:},methods:{sendPost(){// 1、发送post请求axios({url: http://127.0.0.1:8000/data/, //1、请求地址method: post, // 2、请求方法data: Qs.stringify( //3、可选函数、序列化data{ids: [1,2,3],type: admin}, //4、提交数据{ indices: false } // indices: false),responseType: json, //5、返回默认格式jsonheaders: {authorization: xxxtokenidxxxxx},//6、身份验证token})// 2、回调函数.then(resp {console.log(resp.data);})// 3、捕获异常.catch(err {console.log(请求失败err.status,err.statusText);});}}});}post: axios发送post请求6.后端测试接口def data(request):if request.method GET:token_id request.META.get(HTTP_AUTHORIZATION) # header中的tokenidprint(request.GET.getlist(ids)) # 获取get请求中列表data {id:1,name: zhangsan}return HttpResponse(json.dumps(data))elif request.method POST:token_id request.META.get(HTTP_AUTHORIZATION) # header中的tokenidprint(request.POST.getlist(ids)) # 获取post请求中的列表data {id:1,name: zhangsan,method: POST}return HttpResponse(json.dumps(data))views.py后端测试接口#1、qs用途 在 axios中利用QS包装data数据#2、安 装: npm install qs -S#3、常见用法import Qs from qs;Qs.stringify(data);Qs.parse(data)7、vuejs借助axios发送ajax请求(同级目录下创建以下两个文件)1.json{id:1001,name:秋香,age:18}2.html发送AJAX请求发送AJAX请求window.οnlοadfunction(){new Vue({el:#itany,data:{user:{// name:alice,// age:19},uid:},methods:{send(){axios({method:get,url:user.json}).then(function(resp){ // 请求成功调用此函数console.log(resp.data); // {id: 1001, name: 秋香, age: 18}}).catch(resp { // 请求失败调用此函数console.log(请求失败resp.status,resp.statusText);})}}});}index.html8.vuejs借助axios发送get请求server.php//获取参数$name$_POST[name];$age$_POST[age];//响应数据echo 姓名,$name,,年龄,$age;?server.phpindex.html发送AJAX请求GET方式发送AJAX请求window.οnlοadfunction(){new Vue({el:#itany,data:{user:{// name:alice,// age:19},uid:},methods:{sendGet(){ // axios.get(server.php?nametomage23)axios.get(server.php,{params:{name:alice,age:19}}).then(resp {console.log(resp.data);}).catch(err {console.log(请求失败err.status,err.statusText);});},}});}index.html5、vuejs借助axios发送post请求# 1. axios默认发送数据时数据格式是Request Payload并非我们常用的Form Data格式# 2. 所以参数必须要以键值对形式传递不能以json形式传参# 3. 传参方式# 1. 自己拼接为键值对# 2. 使用transformRequest在请求发送前将请求数据进行转换# 3. 如果使用模块化开发可以使用qs模块进行转换1.server.php//获取参数$name$_POST[name];$age$_POST[age];//响应数据echo 姓名,$name,,年龄,$age;?server.php