繁体   English   中英

Nuxt axios无法处理服务器响应

[英]Nuxt axios cannot handle the server sesponse

我是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服务中, WebAPI的响应是undefined

另外,值得一提的是,完全相同的WebAPI类与我在整个应用程序中所做的其他API请求完美配合。

您能解决这个问题吗? 你有什么不对吗?

我认为您使用axios错误。 尝试使用$request方法,像这样:

async call(config) {
  try {
    const result = await this.$http.$request(config);
    console.log(result);
    // ...
  } catch( e) {
    // ...
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM