我试图验证用户,但我无法调用Api由于跨源读取阻止(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;
}
Allow-Control-Allow-Origin:允许您从任何来源请求任何带有ajax的站点。添加到响应'Allow-Control-Allow-Origin:*'标题
在浏览器中添加此扩展程序:Allow-Control-Allow-Origin
您需要在API响应中添加一些CORS标头。
我的后端是PHP,所以我只是添加了这个字符串:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type');
你可以做几件事。一种是在登录后端添加CORS策略。如果你在后端使用Node,那么有一个名为cors的npm包可以解决问题。如果没有,应该有一种简单的方法来在您正在使用的语言/框架中添加策略。如果您通过Firebase或Auth0等服务提供商进行身份验证,则可以将您的URL(localhost或其他任何内容)添加到其设置中,这样就可以解决问题。
您可以做的另一件事是为浏览器添加扩展程序,以便它为您发送cors预检请求。我有一个与Chrome一起使用的产品,它叫做CORS。它工作得很好,但要小心。我只在我在localhost上开发时使用它,所以我将它配置为仅在URL为localhost时添加预检请求才是安全的。
最终,您可能需要的是这两种方法的组合。