vue发送ajax --axios入门介绍
2017-08-12 22:10
477 查看
vue是不支持发送ajax请求的,所以需要借助axios来完成这个工作。
axios下载地址:https://github.com/mzabriskie/axios
基本用法:
axios([options]);
axios.get(url,[options]); //get方式传递参数是用过url传递
axios.post(url,data,[options]); //post方式传递参数通过data属性
注:传参方式区别:
get方式的通过url通过?key=vaule来传参,也可以设置在params属性中。
API案例:
// 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);
}); pos请求参数:虽然是模拟表单提交,实际上也是通过Key=Value的方式传递;所以如果你在传递参数的时候使用json格式的传递参数的时候要使用transformResponse来格式化
API案例:
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
具体的可以参考AIP。
案例:标准方法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>axiso发送ajax请求入门</title>
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/axios/axios.js" ></script>
<script type="text/javascript">
window.onload=function(){
new Vue({
el:'#main',
data:{
},
methods:{
send:function(){
axios({
method:'get',
url:'testData/studen.json',
}).then(function(resp){
console.log(resp.data);
}).catch(resp=>{
console.log("发送失败"+resp.status+","+resp.statusText);
});
}
}
});
}
</script>
</head>
<body>
<div id="main">
<button type="button" @click="send">发送ajax请求</button>
</div>
</body>
</html>
json文件:{"Id":"01","Name":"zwc","sex":"男"}
运行结果:
注:使用过jqery 中ajax的类似,反正本质就是xmlHTTPRequest对象。
跨域问题:
axios本身不支持跨域请求。所以只能使用第三方的库。(解决跨域两种方法:jsonp,CORS)
axios下载地址:https://github.com/mzabriskie/axios
基本用法:
axios([options]);
axios.get(url,[options]); //get方式传递参数是用过url传递
axios.post(url,data,[options]); //post方式传递参数通过data属性
注:传参方式区别:
get方式的通过url通过?key=vaule来传参,也可以设置在params属性中。
API案例:
// 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);
}); pos请求参数:虽然是模拟表单提交,实际上也是通过Key=Value的方式传递;所以如果你在传递参数的时候使用json格式的传递参数的时候要使用transformResponse来格式化
API案例:
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
具体的可以参考AIP。
案例:标准方法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>axiso发送ajax请求入门</title>
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/axios/axios.js" ></script>
<script type="text/javascript">
window.onload=function(){
new Vue({
el:'#main',
data:{
},
methods:{
send:function(){
axios({
method:'get',
url:'testData/studen.json',
}).then(function(resp){
console.log(resp.data);
}).catch(resp=>{
console.log("发送失败"+resp.status+","+resp.statusText);
});
}
}
});
}
</script>
</head>
<body>
<div id="main">
<button type="button" @click="send">发送ajax请求</button>
</div>
</body>
</html>
json文件:{"Id":"01","Name":"zwc","sex":"男"}
运行结果:
注:使用过jqery 中ajax的类似,反正本质就是xmlHTTPRequest对象。
跨域问题:
axios本身不支持跨域请求。所以只能使用第三方的库。(解决跨域两种方法:jsonp,CORS)
相关文章推荐
- vue项目使用axios发送请求让ajax请求头部携带cookie
- vue项目使用axios发送请求让ajax请求头部携带cookie的方法
- Vue--axios:vue中的ajax异步请求(发送和请求数据)、vue-resource异步请求和跨域
- Vue 爬坑之路(六)—— 使用 Vuex + axios 发送请求
- vue中选择 axios 来完成 ajax 请求。
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- axios --Vue使用Ajax
- ajax心得1--ajax入门介绍
- Vue 爬坑之路(六)—— 使用 Vuex + axios 发送请求
- Vue------用axios发送post请求自动set cookie
- DWR 入门介绍-Ajax
- vue2+vuex+vue-router 快速入门(三) vue 实例介绍
- vue 框架的安装和入门介绍
- AJAX入门介绍 - GET 和 POST 配合表单
- springboot vue.js axios.js ajax请求纯HTML页面传值
- vue2+vuex+vue-router 快速入门(四) vue-router 介绍
- VUE 更好的 ajax 上传处理 axios.js实现代码
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- AJAX 入门介绍 - 整合 XML 和 DOM
- ajax 或者 axios 或调函数中 指向vue 实例