Hi everyone!
I have a problem when I am trying to do a Http call using capacitor (iOS platform).
The error is the following:
[Error] XMLHttpRequest cannot load https://… due to access control checks.
Failed to load resource: Preflight response is not successful
I think that the issue is related to the origin, in that case is: capacitor://localhost, so I included that origin in the backend side as allowed origin but it is still not working…
I have the service hosted in Azure Cloud and when I am trying to set the cors rules it doesn’t allow origins starts with capacitor://… only allows http:// or https://.
Debug your app locally on a device, and trigger calls to Azure. From DevTools, open the Network tab to look at those Azure API calls - the preflight failures should show more detail. Might give you something to go off of
If nothing else works, you can always remove all Allowed Origins and use asterisk/star, as Azure mentions in your screenshot above: * However, that’s not the best for security so don’t do that if this is a serious/business app.
Thanks for your answer @netkow!
However, I already applied your solution.
After some research I found the following topic in Azure:
The point is that iOS uses a diferent origin than Android, thus, Azure doesn’t support it in the CORS section.
I would like to know why iOS uses capacitor://localhost instead of http://localhost like Android.
Is there any possible solution or workarround?
We’ll try to get in touch with the Azure team. As a workaround, check out the HTTP plugin, which uses native HTTP connections (aka bypassing this issue entirely, I think).
@netkow I tryied using the plugin but it seems that the origin is the same “capacitor://localhost” which means that can’t pass throug Azure CORS. Do you know if there is another workaround or if a can change the origin header to allow the request pass through CORS?
The solution to this issue is from the Azure side. Use the Azure CLI to add the CORS origin, rather than doing it via the Azure portal UI. The CLI call is:
az webapp cors add --allowed-origins https://myapps.com --name MyWebApp --resource-group MyResourceGroup --subscription MySubscription
Another alternative is to use the Azure Resources Explorer: https://resources.azure.com/subscriptions to edit the configuration files directly. Basically you can expand to yourAppName -> config -> web, edit the CORS and then PUT your changes.
Great solution!
For those interested, the solution is still working on 2023.
It worked for me using this variation of the command:
az webapp cors add --resource-group {myRG} --name {myAppName} --allowed-origins capacitor://localhost