axios回调函数的参数res:
回调函数的res并不是接口返回的数据,而是表示一个响应对象。res.data才表示接口响应的数据。
回调函数的res并不是接口返回的数据,而是表示一个响应对象。res.data才表示接口响应的数据。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/axios.min.js" ></script>
</head>
<body>
<div id="container">
<button type="button" @click="test1">测试1</button>
</div>
<script type="text/javascript">
var vm = new Vue({
el:"#container",
methods:{
test1:function(){
axios.get('http://localhost:9999/music/detail?id=25640392').then(function(res){
//回调函数的res并不是接口返回的数据,而是表示一个响应对象。res.data才表示接口响应的数据。
console.log(res);
});
// axios.all([listMusics(),getMusicDetail()])
// .then(axios.spread(function (acct, perms) {
// // 两个请求现在都执行完成
// console.log(res1);
// console.log(res2);
// }));
}
}
});
function listMusics() {
return axios.get('http://localhost:9999/music/search?s=成都');
}
function getMusicDetail() {
return axios.get('http://localhost:9999/music/detail?id=25640392');
}
</script>
</body>
</html>
res响应对象:
res.data才表示接口响应的数据。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/axios.min.js" ></script>
</head>
<body>
<div id="container">
<button type="button" @click="test1">测试1</button>
</div>
<script type="text/javascript">
var vm = new Vue({
el:"#container",
methods:{
test1:function(){
axios.get('http://localhost:9999/music/detail?id=25640392').then(function(res){
//回调函数的res并不是接口返回的数据,而是表示一个响应对象。res.data才表示接口响应的数据。
console.log(res.data);
});
// axios.all([listMusics(),getMusicDetail()])
// .then(axios.spread(function (acct, perms) {
// // 两个请求现在都执行完成
// console.log(res1);
// console.log(res2);
// }));
}
}
});
function listMusics() {
return axios.get('http://localhost:9999/music/search?s=成都');
}
function getMusicDetail() {
return axios.get('http://localhost:9999/music/detail?id=25640392');
}
</script>
</body>
</html>
只取song.name的方法一(普通方法):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/axios.min.js" ></script>
</head>
<body>
<div id="container">
<table>
<tr>
<td>{{song.name}}</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
<button type="button" @click="test1">测试1</button>
</div>
<script type="text/javascript">
var vm = new Vue({
el:"#container",
data:{
song:{
}
},
methods:{
test1:function(){
axios.get('http://localhost:9999/music/detail?id=25640392').then(function(res){
//回调函数的res并不是接口返回的数据,而是表示一个响应对象。res.data才表示接口响应的数据。
if(res.data.code == 200){
vm.song = res.data.songs[0];
}
});
// axios.all([listMusics(),getMusicDetail()])
// .then(axios.spread(function (acct, perms) {
// // 两个请求现在都执行完成
// console.log(res1);
// console.log(res2);
// }));
}
}
});
function listMusics() {
return axios.get('http://localhost:9999/music/search?s=成都');
}
function getMusicDetail() {
return axios.get('http://localhost:9999/music/detail?id=25640392');
}
</script>
</body>
</html>
只取song.name的方法二(_this):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/axios.min.js" ></script>
</head>
<body>
<div id="container">
<table>
<tr>
<td>{{song.name}}</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
<button type="button" @click="test1">测试1</button>
</div>
<script type="text/javascript">
var vm = new Vue({
el:"#container",
data:{
song:{
}
},
methods:{
test1:function(){
var _this = this;
axios.get('http://localhost:9999/music/detail?id=25640392').then(function(res){
//回调函数的res并不是接口返回的数据,而是表示一个响应对象。res.data才表示接口响应的数据。
if(res.data.code == 200){
_this.song = res.data.songs[0];
}
});
// axios.all([listMusics(),getMusicDetail()])
// .then(axios.spread(function (acct, perms) {
// // 两个请求现在都执行完成
// console.log(res1);
// console.log(res2);
// }));
}
}
});
function listMusics() {
return axios.get('http://localhost:9999/music/search?s=成都');
}
function getMusicDetail() {
return axios.get('http://localhost:9999/music/detail?id=25640392');
}
</script>
</body>
</html>
只取song.name的方法三(箭头函数):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/axios.min.js" ></script>
</head>
<body>
<div id="container">
<table>
<tr>
<td>{{song.name}}</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
<button type="button" @click="test1">测试1</button>
</div>
<script type="text/javascript">
var vm = new Vue({
el:"#container",
data:{
song:{
}
},
methods:{
test1:function(){
//var _this = this;
axios.get('http://localhost:9999/music/detail?id=25640392').then( (res)=>{
//回调函数的res并不是接口返回的数据,而是表示一个响应对象。res.data才表示接口响应的数据。
if(res.data.code == 200){
this.song = res.data.songs[0];
}
});
// axios.all([listMusics(),getMusicDetail()])
// .then(axios.spread(function (acct, perms) {
// // 两个请求现在都执行完成
// console.log(res1);
// console.log(res2);
// }));
}
}
});
function listMusics() {
return axios.get('http://localhost:9999/music/search?s=成都');
}
function getMusicDetail() {
return axios.get('http://localhost:9999/music/detail?id=25640392');
}
</script>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/128132.html