我是Nuxt.js
的新手,我遇到了一个奇怪的问题。我的后端API中有一个端点,允许最终用户发送token
和新的password
并重置用户密码。
在正确发送请求的同时,服务器使用正确的数据进行响应:
在Nuxt.js
方面,我有一个问题,有响应数据。
因此,为了使用axios处理所有HTTP请求,我有一个这样的类:
class WebAPI {
// $axios is the instance used in the Nuxt.js context
constructor($axios) {
this.$http = $axios;
}
async call(config) {
try {
///////////////////////////////////////////////////////
// HERE IS THE PROBLEM. THE FOLLOWING CONSOLE.LOG
// IT RETURNS undefined WHILE THE NETWORK RESPONSE
// RETURNS WITH DATA
///////////////////////////////////////////////////////
const result = await this.$http(config);
console.log(result);
// ...
} catch( e) {
// ...
}
}
}
我使用这个类如下:
const data = {
token,
new_password
};
const options = {
method: 'POST',
url : '/reset-password',
data
};
return this.webApi.call(options);
但正如你可能看到的那样,在WebAPI
服务中,axios的响应是undefined
。
此外,值得一提的是,完全相同的WebAPI
类与我在整个应用程序中执行的其他API请求完美配合。
你能帮忙解决这个问题吗?你觉得有什么不对吗?
我认为你使用axios错了。尝试使用$request
方法,如下所示:
async call(config) {
try {
const result = await this.$http.$request(config);
console.log(result);
// ...
} catch( e) {
// ...
}
}