简体   繁体   English

Nuxt axios无法处理服务器响应

[英]Nuxt axios cannot handle the server sesponse

I am new to Nuxt.js and I am faced with a strange kind of issue. 我是Nuxt.js ,但我遇到了一种奇怪的问题。 I have an endpoint in my backend API, allowing the end user to send a token and a new password and reset the user password. 我的后端API中有一个端点,允许最终用户发送token和新password并重置用户密码。

While the request is sent correctly, and the server responding with the correct data: 当请求发送正确,并且服务器以正确的数据响应时:

在此处输入图片说明

In the Nuxt.js side, I have an issue, with the response data. Nuxt.js方面,响应数据存在问题。

So, to handle all the HTTP requests using the axios, I have a class like that: 因此,要使用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) {
            // ...
        }
    }
}

And I use this class like: 我像这样使用此类:

const data = {
    token,
    new_password
};

const options = {
    method: 'POST',
    url   : '/reset-password',
    data
};

return this.webApi.call(options);

But as you probably see, in the WebAPI service, the response of the axios is undefined . 但是,您可能已经看到,在WebAPI服务中, WebAPI的响应是undefined

Also, it worths to mention, that the exact same WebAPI class working perfectly with other API Requests I do throughout the application. 另外,值得一提的是,完全相同的WebAPI类与我在整个应用程序中所做的其他API请求完美配合。

Could you help with that issue? 您能解决这个问题吗? Do you see anything wrong? 你有什么不对吗?

I think you are using axios wrong. 我认为您使用axios错误。 Try use $request method, like that: 尝试使用$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