Nuxt axios无法处理服务器sesponse

问题描述 投票:0回答:1

我是Nuxt.js的新手,我遇到了一个奇怪的问题。我的后端API中有一个端点,允许最终用户发送token和新的password并重置用户密码。

在正确发送请求的同时,服务器使用正确的数据进行响应:

enter image description here

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请求完美配合。

你能帮忙解决这个问题吗?你觉得有什么不对吗?

javascript vue.js vuejs2 nuxt.js nuxt
1个回答
1
投票

我认为你使用axios错了。尝试使用$request方法,如下所示:

async call(config) {
  try {
    const result = await this.$http.$request(config);
    console.log(result);
    // ...
  } catch( e) {
    // ...
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.