Chrome的跨源读取阻止(CORB)阻止了跨源响应

时间:2019-02-19 13:59:15

标签: angular ionic-framework ionic2 ionic3

我正在尝试验证用户身份,但由于 跨域读取阻止(CORB)被阻止,发出我的login.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());
        }

this.auth.login函数

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;
    }

3 个答案:

答案 0 :(得分:2)

允许控制-允许原点: 允许您从任何来源请求带有ajax的任何站点。添加到响应'Allow-Control-Allow-Origin:*'标头

在浏览器中添加此扩展程序:Allow-Control-Allow-Origin

答案 1 :(得分:1)

您需要在API响应中添加一些CORS标头。

我的后端在PHP中,所以我刚添加了以下字符串:

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

答案 2 :(得分:0)

您可以做几件事。一种是将CORS策略添加到您的登录后端。如果您使用Node作为后端,则有一个名为cors的npm软件包可以解决这个问题。如果没有,应该有一种简单的方法来以您使用的语言/框架添加策略。如果您通过Firebase或Auth0之类的服务提供商进行身份验证,则可以将URL(localhost或其他名称)添加到它们的设置中,即可解决该问题。

您可以做的另一件事是向浏览器添加一个扩展,以便它为您发送cors预检请求。我有一个可与Chrome一起使用的软件,它的名称为CORS。它很好用,但是要小心。我只在本地主机上开发时才使用它,因此我将其配置为仅在URL为本地主机时才添加预检请求。

最终,您可能需要将这两种方法结合起来。