I am creating an API with our API server script and am trying to communicate with the API on the IONIC framework application. I am working but it keeps on bringing up the cross-origin blocked error:
The correct and easiest solution is to enable CORS by returning the right response headers from the web server or backend and responding to preflight requests, as it allows to keep using XMLHttpRequest , fetch , or abstractions like HttpClient in Angular.
Cross-Origin Resource Sharing (CORS) errors occur when a server doesn't return the HTTP headers required by the CORS standard. To resolve a CORS error from an API Gateway REST API or HTTP API, you must reconfigure the API to meet the CORS standard.
To code to set the CORS configuration globally in main Spring Boot application is given below. Now, you can create a Spring Boot web application that runs on 8080 port and your RESTful web service application that can run on the 9090 port.
Open a network tab in your console. In the response header look for the Access-Control-Allow-Origin header. If it does not exist then add it as a middleware in the way we discussed above. If it does exist then make sure there is no URL mismatch with the website.
When developing locally using ionic serve
or ionic run/emulate -l -c
with livereload enabled, ionic creates a local dev server at port 8100 by default (http://localhost:8100/
). The origin
in this case is localhost:8100
, which when you contact an external service via HTTP with CORS enabled, they deem the request not trustworthy and therefor reject it.
As suggested by Ionic themselves (http://blog.ionic.io/handling-cors-issues-in-ionic/) , you can create a proxy alias within the Ionic app to route the API calls via, avoiding the origin
issue altogether, however, their guide was specific for Ionic 1, so here is an update for Ionic v2.
Open ionic.config.json
and add in the following proxies
setup.
{
"name": "project-name",
"app_id": "xyz-projectid",
"v2": true,
"typescript": true,
"proxies": [{
"path": "/api",
"proxyUrl": "https://the-real-api-host.com"
}]
}
In this instance, we are creating a path within the ionic app at /api
,which will forward requests to the endpoint https://the-real-api-host.com
.
If you wanted to use a different api endpoint, for example http://my-custom-api.com/api/v2/
, you could insert it into proxyUrl
instead.
In your app code, you now need to update all the references of the base URL for the API endpoint at https://the-real-api-host.com
with /api
. A call to /api
should be detected when in Ionic serve, and proxied to the real address.
Each projects implementation may vary. In my case, I did not have control of the API, as it was an external service, so I could not control the CORS handling/responses myself.
Note: remember to restart the server (ionic serve
) or you will get 404's from your API call because it will not yet be proxying.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With