Ionic Chrome“跨来源读取阻止(CORB)阻止跨来源响应”离子

20jt8wwn  于 2022-12-16  发布在  Ionic
关注(0)|答案(3)|浏览(293)

我正在尝试验证用户,但由于跨源读取阻塞(CORB)被阻止问题,我无法调用API。ts代码为

if (this.plugins.isOnline()) {
            if (this.wait == true) {
                return;
            } else if (this.userLogin.email == '' || this.userLogin.password == '') {
                this.utility.doToast("Please don't leave any field blank.");
                return;
            } else {
                this.wait = true;
                this.auth.login(this.userLogin).subscribe((success) => {
                    this.wait = false;
                    console.log(success.successData);
                    this.credential.setUser(success.successData);
                    this.plugins.sendTags(success.successData.id)
                    this.rememberUsertype(success.successData.is_celebrity);
                    if(success.successData.is_celebrity == '0'){
                    this.app.getRootNav().setRoot("HomePage");
                    }
                    else if(success.successData.is_celebrity == '1'){
                    this.app.getRootNav().setRoot("DashboardPage");
                     }

                    }, (error) => {
                        console.log(error);
                    this.wait = false;
                    if (this.utility.timeOutResponse(error))
                        this.utility.doToast("The email or password you entered is incorrect.")
                })
            }
        } else {
            this.utility.doToast(this.utility.internetConnectionMessage());
        }

此.auth.登录函数

login(params) {
        console.log(params);
        var url = this.constants.API_ENDPOINT + 'login';
        var response = this.http.post(url, params, {}).map(res => res.json());
        return response;
    }
toiithl6

toiithl61#

允许-控制-允许-原点:允许您从任何来源请求任何带有 AJAX 的站点。添加到响应'Allow-Control-Allow-Origin:*'标题
在浏览器中添加此扩展:允许-控制-允许-原点

ztigrdn8

ztigrdn82#

您需要在API响应中添加一些CORS标头。
我的后端是PHP,所以我添加了以下字符串:

header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
    header('Access-Control-Allow-Headers: Content-Type');
gorkyyrv

gorkyyrv3#

您可以做几件事。一件是将CORS策略添加到登录后端。如果您使用Node作为后端,则有一个名为cors的npm包可以完成此操作。如果没有,则应该有一种简单的方法以您使用的语言/框架添加策略。如果您通过Firebase或Auth0等服务提供商进行身份验证,则可以添加您的URL(本地主机或其他)到他们的设置,这将解决它。
你可以做的另一件事是添加一个扩展到你的浏览器,这样它就可以发送cors preflight请求给你。我有一个我使用的Chrome浏览器,它只是所谓的CORS。它的工作很好,但要小心。我只使用它时,我开发的localhost,所以我配置它只添加preflight请求时,网址是localhost只是为了安全。
最终,也许你需要的是这两种方法的结合。

相关问题