如何使用离子旁路铬中的CORB

时间:2019-02-19 12:09:35

标签: angular ionic-framework ionic2 ionic3

当我调用登录api时,我得到MIME类型application / json 的跨域读取阻止(CORB)阻止了跨域响应 ,但是我无法验证用户,每当我尝试登录用户时,CORB都会阻止我的http请求,并且无法验证该用户。我是ionic的新用户,所以我无法解决此错误,请提供帮助

  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.getRequiremensts();
                this.auth.login(this.userLogin).subscribe((success) => {
                    this.wait = false;
                    console.log("loginData",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());
        }



login(params) {

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


 if (options == null) {
            options = new RequestOptions();
        }
        if (options.headers == null) {
            options.headers = new Headers();
        }
        options.headers.append('app_key', 'Some App Key');

        var _token = localStorage.getItem('token');
        if (_token)
            options.headers.set('session_token', _token);

        options.headers.set("Cache-Control", "no-cache");

        return options; 
    }

错误

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Response.Body.json (http.js:1091)
    at MapSubscriber.project (auth.service.ts:29)
    at MapSubscriber._next (map.js:79)
    at MapSubscriber.Subscriber.next (Subscriber.js:93)
    at CatchSubscriber.Subscriber._next (Subscriber.js:129)
    at CatchSubscriber.Subscriber.next (Subscriber.js:93)
    at TimeoutSubscriber.Subscriber._next (Subscriber.js:129)
    at TimeoutSubscriber._next (timeout.js:132)
    at TimeoutSubscriber.Subscriber.next (Subscriber.js:93)

1 个答案:

答案 0 :(得分:0)

可能的重复项:Chrome' Cross-Origin Read Blocking (CORB) blocked cross-origin response' ionic

反正...

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

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

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