Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taro.request 失败情况小程序和H5不一致 #4679

Closed
hyacinthus opened this issue Oct 24, 2019 · 6 comments
Closed

Taro.request 失败情况小程序和H5不一致 #4679

hyacinthus opened this issue Oct 24, 2019 · 6 comments
Assignees

Comments

@hyacinthus
Copy link

问题描述
小程序只要请求网络成功,不管status code,都会成功。
而现在 h5 里边 Taro.request 在非2xx的时候会失败

虽然可以写两遍http状态码的处理,但是还是太麻烦了。

@taro-bot
Copy link

taro-bot bot commented Oct 24, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@taro-bot
Copy link

taro-bot bot commented Oct 24, 2019

CC @Littly

@Chen-jj Chen-jj added the API label Oct 24, 2019
@fjc0k
Copy link
Contributor

fjc0k commented Oct 24, 2019

暂时可以试试 taro-axios:https://github.com/fjc0k/taro-axios

@hyacinthus
Copy link
Author

我发现这个问题是从最近的版本才出现的,因为我升级把一个H5项目升挂了……
之前应该是统一的,我之前判断 statusCode 现在没有了。。

@dobyte
Copy link

dobyte commented Feb 26, 2020

解决方案哈,希望对您有帮助。

        let res: any = {};

        await Taro.request({
            url: params.url,
            data: params.data,
            method: params.method,
            header: params.header,
            dataType: params.dataType,
            mode: config.mode
        }).then((response: any) => {
            res = response;
        }).catch((response: any) => {
            res.statusCode = response.status;

            res.header = {};

            response.headers.forEach((val: any, key: string) => {
                res.header[key] = val
            });

            if (params.responseType === 'arraybuffer') {
                return response.arrayBuffer();
            }

            if (params.dataType === 'json' || typeof params.dataType === 'undefined') {
                return response.json();
            }

            if (params.responseType === 'text') {
                return response.text();
            }

            return Promise.resolve(null);
        }).then(data => {
            res.data = data;

            return res;
        });

@hyacinthus
Copy link
Author

@Yuebanfuxiao 谢谢,处理谁都会处理,我报告的是 API 行为不一致。

@Chen-jj Chen-jj removed the API label Jul 3, 2020
@Chen-jj Chen-jj closed this as completed Jul 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants