问题描述
我目前正在开发基于 ionic 3.9.2 的应用。我正在尝试向 API 发送 POST 请求,但收到 403 禁止。最初我遇到了 CORS 问题,但我暂时将 https://cors-anywhere.herokuapp.com/
添加到 URL 以绕过该问题。这是函数:
凭证详细信息.ts:
editVoucher(voucher) {
voucher.Comments = (voucher.Comments || " ");
if (!voucher.Comments.includes('Edited via Guide App')) {
voucher.Comments += " Edited via Guide App";
}
//WORKS UP TILL HERE
this.voucherService.editVoucher(voucher)
// .map((res) => res.json())
.map((res) => res)
.subscribe(
(res) => {
console.log("old voucher id",voucher.voucher_Id);
console.log("new voucher id",res);
this.showSuccess("Voucher updated successfully");
},(err) => {
console.log("Error occured while updating voucher id "),this.showError("Oops,an error occured. Please try again");
}
);
}
它所引用的函数 voucherService.editVoucher(voucher)
在这里:
凭证.ts:
public editVoucher(voucherToUpdate) {
//application/x-www-form-urlencoded
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',// Authorization: 'my-auth-token'
})
};
return this.HttpClient.post(`https://cors-anywhere.herokuapp.com/${API_BASE_URL}/Api/GuideApp/UpdateVoucher`,{
Voucher: voucherToUpdate,tenantId: voucherToUpdate.tenantId
},httpOptions);
}
解决方法
让我们承认您的代码很好,并且您根据服务器的需要传递了您的身份验证凭据。 cors-anywhere
有一些限制 as mentioned here:
从 2 月 1 日开始。 2021 年,cors-anywhere.herokuapp.com 将只提供服务 访问者完成挑战后的请求:用户 (开发人员)必须访问 cors-anywhere.herokuapp.com 上的页面才能 暂时解锁他们浏览器的演示。这允许开发人员 试用功能,帮助决定自托管或 寻找替代品。
尝试在浏览器上手动解锁:https://cors-anywhere.herokuapp.com/corsdemo