
The browser automatically sends the appropriate headers for CORS in every request to the server, including the preflight requests. Indicates how long the results of a preflight request can be cached. Specifies the headers that the browser is allowed to access.

Whether or not the request can be made with credentials. Used in response to a preflight request to indicate which headers can be used when making the actual request, aside from the simple headers, which are always allowed. Which methods are allowed when accessing the resource: GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH. Specifies the origin to be allowed, like or * to allow all origins. CORS Headers Server Headers (Response) In our example API, GET requests don't need to be preflighted because no JSON data is being sent, and so the app doesn't need to use the Content-Type: application/json header. No ReadableStream or event listeners in XMLHttpRequestUpload are used.Some requests are always considered safe to send and don't need a preflight if they meet all of the following conditions:

In our example, since the API expects JSON, all POST requests will have a Content-Type: application/json header and always be preflighted. Otherwise, the request will be made after the preflight. If the returned origin and method don't match the ones from the actual request, or any of the headers used are not allowed, the request will be blocked by the browser and an error will be shown in the console. The preflight request would be like this (some default headers omitted for clarity):Īccess-Control-Allow-Origin: Access-Control-Allow-Methods: GET, POST, OPTIONSĪccess-Control-Allow-Headers: Content-Type Let's suppose we are making a POST request to a fictional JSON API at with a Content-Type of application/json. If any of the conditions above are met, a preflight request with the OPTIONS method is sent to the resource URL.

No 'Access-Control-Allow-Origin' header is present on the requested resource. ) don't match, the browser's Same Origin Policy takes effect and CORS is required for the request to be made.ĬORS errors are common in web apps when a cross-origin request is made but the server doesn't return the required headers in the response (is not CORS-enabled): with ionic serve) and the origin of the resource being requested (e.g. When the origin where your app is served (e.g. For example, apps running in Capacitor have capacitor://localhost (iOS) or (Android) as their origin. In order to know if an external origin supports CORS, the server has to send some special headers for the browser to allow the requests.Īn origin is the combination of the protocol, domain, and port from which your Ionic app or the external resource is served.

Cross-Origin Resource Sharing (CORS) is a mechanism that browsers and webviews - like the ones powering Capacitor and Cordova - use to restrict HTTP and HTTPS requests made from scripts to resources in a different origin for security reasons, mainly to protect your user's data and prevent attacks that would compromise your app.
