Troubleshoot CORS policy errors
Fix a request that the browser blocks under the CORS policy by adding the Access-Control-Allow-Origin header with a Response Phase rule.
This page covers a cross-origin request that the browser blocks under the Cross-Origin Resource Sharing (CORS) policy, when the response comes from an application on Azion. To send CORS headers before an error shows up, refer to Enable CORS on an application.
A request is blocked with No Access-Control-Allow-Origin header is present
A page on one domain requests a resource on another domain, and the browser blocks the response.
CORS uses HTTP headers to grant a page access to resources on a different origin server. The browser blocks the response because it carries no Access-Control-Allow-Origin header for the requesting origin.
The example in this section uses two domains:
http://a.domain.com/, the domain that serves the resource.http://b.domain.com/, the domain of the page that requests it.
The browser reports the error in this form:
Confirm the error in the browser
To find the blocked request in Google Chrome:
- Select View > Developer > Developer tools.
- Select the Network tab.
- Select the first line, page-cors-subdomain.a.html, and read its Request URL in the right panel.
- On the page, select Test CORS on click. A red line appears in the Name column.
- Hover over the page-cors-subdomain-a.txt line and read the tooltip.
- Select page-cors-subdomain-a.txt and read its details in the right panel.
- Select the Console tab, then Errors. The CORS error line names the origin domain and the destination domain.
Add the header with a rule
The fix is a Response Phase rule on the application of domain A. The rule returns the requesting origin in the Access-Control-Allow-Origin header when that origin is domain B. For the format a header argument takes, refer to Rules Engine for Applications.
To create the rule in Azion Console:
Access Azion Console > Applications, then select the application of domain A, a.domain.com.
In the General section, enter a Name.
In the Phase section, select Response Phase.
In the Criteria section, select ${http_origin} as the variable and is_equal as the operator, and enter http://b.domain.com as the argument. Do not add a trailing slash (/).
In the Behaviors section, select Add Response Header.
Enter Access-Control-Allow-Origin: ${http_origin} as the argument.
The rule appears in the Rules Engine tab, under Response.
Verify the fix
To test the rule, serve two files from domain A. The page page-cors-subdomain-a.html requests the text file when you select its button:
The file page-cors-subdomain-a.txt holds the text the page loads:
To check the result in Google Chrome:
- Select View > Developer > Developer tools.
- Select the Network tab.
- Select the first line, page-cors-subdomain.a.html, and read its details in the right panel.
- On the page, select Test CORS on click. The page shows
You clicked! I belong to the domain a. Domain b shall call me. - Select the Console tab, then Errors.
The Errors list shows no CORS error, and the response from domain A reaches the page of domain B.
Chrome browser is a registered trademark of Google LLC in the United States and/or other countries.