使用从Axios检索的数据调用
问题描述:
我试图使用从axios调用中检索到的数据。我在响应中获得了正确的信息,但是当我尝试返回响应时,我在调用函数中得到了未定义的信息。有没有不同的方式将response.data返回给调用函数?使用从Axios检索的数据调用
static getRequest(url) {
require('es6-promise').polyfill();
axios({
method: 'get',
url: url,
responseType:'json',
withCredentials: true
})
.then(response => {
console.log(response.data);
return response.data;
})
.catch(error => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log('___________ERROR RESPONSE__________');
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log('_________ERROR REQUEST_______');
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an
Error
console.log('Error', error.message);
}
console.log('_________ERROR CONFIG_________');
console.log(error.config);
});
}
答
您还需要从getRequest
函数返回的axios
电话。
在上面的代码中,您只能返回您的axios诺言。调用getRequest
时,以下代码将返回response
的值。
static getRequest(url) {
return axios({
method: 'get',
url: url,
responseType:'json',
withCredentials: true
}).then(response => {
return response.data
})
//rest of code here
}
这是工作谢谢。 – Totals