Vue.js 如何在vuejs中从axios response访问响应数据
我的服务器Api响应如下所示:-Vue.js 如何在vuejs中从axios response访问响应数据,vue.js,axios,Vue.js,Axios,我的服务器Api响应如下所示:- {"data":{"databases":["rohit_one_test"]},"error":null,"success":true} showdblist(){ this.url=API_LOCATION+"/tools/mysql/resources/databases/"+this.domain; this.dbs=axiosGet(this.url); console.log(this.dbs); } 我正在
{"data":{"databases":["rohit_one_test"]},"error":null,"success":true}
showdblist(){
this.url=API_LOCATION+"/tools/mysql/resources/databases/"+this.domain;
this.dbs=axiosGet(this.url);
console.log(this.dbs);
}
我正在使用axios和vue js进行这样的调用
//axiosget函数
import axios from 'axios';
export function axiosGet (url) {
return axios.get(url,{ headers: {'Authorization': 'Basic token'}})
.then(function (response) {
return response.data.data;
})
.catch(function (error) {
return 'An error occured..' + error;
})
}
我在别的地方这样称呼它:-
{"data":{"databases":["rohit_one_test"]},"error":null,"success":true}
showdblist(){
this.url=API_LOCATION+"/tools/mysql/resources/databases/"+this.domain;
this.dbs=axiosGet(this.url);
console.log(this.dbs);
}
当我记录dbs变量时,它是这样的
屏幕截图如下:--
我的问题是如何在dbs变量中访问数据库的名称?这是一个承诺,因此您从承诺解析中返回什么都不做。相反,将其视为回报的承诺,并从中做出如下决定:
async showdblist() {
this.dbs = await axiosGet(this.url)
// now this.dbs is correct
}
如果无法使用async/Wait,请将其视为常规承诺:
axiosGet(this.url)
.then((response) => {
this.dbs = response.data.data
})
好吧,这是一个被回报的承诺,所以你从承诺内的回报没有任何作用。相反,将其视为回报的承诺,并从中做出如下决定:
async showdblist() {
this.dbs = await axiosGet(this.url)
// now this.dbs is correct
}
如果无法使用async/Wait,请将其视为常规承诺:
axiosGet(this.url)
.then((response) => {
this.dbs = response.data.data
})
是否有一种首选的、有效的方法,或者上述两种方法中的任何一种都适合使用?Thanks@Aseem你只需要选择你最喜欢的模式。有没有一种更喜欢的、有效的方法,或者以上两种方法中的任何一种都可以使用?Thanks@Aseem你只需选择你最喜欢的图案。