1、 简介php
一、vue自己不支持发送AJAX请求,须要使用vue-resource(vue1.0版本)、axios(vue2.0版本)等插件实现html
二、axios是一个基于Promise的HTTP请求客户端,用来发送请求,也是vue2.0官方推荐的,同时再也不对vue-resource进行更新和维护vue
三、参考:GitHub上搜索axios,查看API文档
2、 使用axios发送AJAX请求
一、安装axios并引入node
一、npm install axios -S #直接下载axios组件,下载完毕后axios.js就存放在node_modules\axios\dist中ios
二、网上直接下载axios.min.js文件npm
三、经过script src的方式进行文件的引入
二、发送get请求json
一、基本使用格式 axios
格式1:axios([options]) #这种格式直接将全部数据写在options里,options实际上是个字典跨域
格式2:axios.get(url[,options]);ide
二、传参方式:
经过url传参
经过params选项传参
三、案例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>发送AJAX请求</title> <script src="js/vue.js"></script> <script src="js/axios.min.js"></script> <script> window.onload=function(){ new Vue({ el:'#itany', data:{ user:{ name:'alice', age:19 }, }, methods:{ send(){ axios({ method:'get', url:'http://www.baidu.com?name=tom&age=23' }).then(function(resp){ console.log(resp.data); }).catch(resp => { console.log('请求失败:'+resp.status+','+resp.statusText); }); }, sendGet(){ axios.get('server.php',{ params:{ name:'alice', age:19 } }) .then(resp => { console.log(resp.data); }).catch(err => { // console.log('请求失败:'+err.status+','+err.statusText); }); }, } }); } </script> </head> <body> <div id="itany"> <button @click="send">发送AJAX请求</button> <button @click="sendGet">GET方式发送AJAX请求</button> </div> </body> </html>
三、发送post请求(push,delete的非get方式的请求都同样)
一、基本使用格式
格式:axios.post(url,data,[options]);
二、传参方式
一、本身拼接为键值对
二、使用transformRequest,在请求发送前将请求数据进行转换
三、若是使用模块化开发,可使用qs模块进行转换
四、注释:axios默认发送post数据时,数据格式是Request Payload,并不是咱们经常使用的Form Data格式,因此参数必需要以键值对形式传递,不能以json形式传参
三、案例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>发送AJAX请求</title> <script src="js/vue.js"></script> <script src="js/axios.min.js"></script> <script> window.onload=function(){ new Vue({ el:'#itany', data:{ user:{ name:'alice', age:19 }, }, methods:{ sendPost(){ // axios.post('server.php',{ // name:'alice', // age:19 // }) //该方式发送数据是一个Request Payload的数据格式,通常的数据格式是Form Data格式,全部发送不出去数据 // axios.post('server.php','name=alice&age=20&') //方式1经过字符串的方式发送数据 axios.post('server.php',this.user,{ //方式2经过transformRequest方法发送数据,本质仍是将数据拼接成字符串 transformRequest:[ function(data){ let params=''; for(let index in data){ params+=index+'='+data[index]+'&'; } return params; } ] }) .then(resp => { console.log(resp.data); }).catch(err => { console.log('请求失败:'+err.status+','+err.statusText); }); }, } }); } </script> </head> <body> <div id="itany"> <button @click="send">发送AJAX请求</button> <button @click="sendGet">GET方式发送AJAX请求</button> </div> </body> </html>
四、发送跨域请求
一、须知:axios自己并不支持发送跨域的请求,没有提供相应的API,做者也暂没计划在axios添加支持发送跨域请求,因此只能使用第三方库
二、使用vue-resource发送跨域请求
三、 安装vue-resource并引入
npm info vue-resource #查看vue-resource 版本信息
cnpm install vue-resource -S #等同于cnpm install vue-resource -save
四、 基本使用方法(使用this.$http发送请求)
this.$http.get(url, [options])
this.$http.head(url, [options])
this.$http.delete(url, [options])
this.$http.jsonp(url, [options])
this.$http.post(url, [body], [options])
this.$http.put(url, [body], [options])
this.$http.patch(url, [body], [options])
五、案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>发送AJAX请求</title> <script src="js/vue.js"></script> <script src="js/axios.js"></script> <script src="js/vue-resource.js"></script> </head> <body> <div id="itany"> <a>{{name}}</a> <button v-on:click="send">sendJSONP</button> </div> </body> <script> new Vue({ el: '#itany', data:{ name: 'alice', age: 19 }, methods:{ send:function(){ // https://sug.so.360.cn/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word=a this.$http.jsonp('https://sug.so.360.cn/suggest', {params:{ word:'a' }} ).then(function (resp) { console.log(resp.data) }) } } }) </script> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>发送AJAX请求</title> <script src="js/vue.js"></script> <script src="js/axios.js"></script> <script src="js/vue-resource.js"></script> </head> <body> <div id="itany"> <a>{{name}}</a> <button v-on:click="send">sendJSONP</button> </div> </body> <script> new Vue({ el: '#itany', data:{ name: 'alice', age: 19 }, methods:{ send:function(){ // https://sug.so.360.cn/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word=a this.$http.jsonp('https://sug.so.360.cn/suggest', {params:{ word:'a' }} ).then(resp=> { console.log(resp.data) }) } } }) </script> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>发送AJAX请求</title> <script src="js/vue.js"></script> <script src="js/axios.js"></script> <script src="js/vue-resource.js"></script> </head> <body> <div id="itany"> <button v-on:click="send">向百度搜索发送JSONP请求</button> </div> </body> <script> new Vue({ el:'#itany', data:{ name:'za' }, methods:{ send:function () { this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su', {params:{wd:'a'}, jsonp:'cb', //百度使用的jsonp参数名为cb,因此须要修改,默认使用的是callbakc参数就不用修改 }).then(function (resp) { console.log(resp.data) }).catch(function (err) { console.log(err) }) } } }) </script> </html>