cancel
Showing results for 
Search instead for 
Did you mean: 

Spartacus Storefront

how can i unblock the CORS policy and get access to electronics site. I am getting the following error.

Access to XMLHttpRequest at 'https://localhost:9002/rest/v2/electronics-spa/cms/pages?fields=DEFAULT&pageType=ContentPage&pageLabelOrId=homepage⟨=en&curr=USD' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field x-anonymous-consents is not allowed by Access-Control-Allow-Headers in preflight response.

0 Kudos

In my local, I am able to solve this issue by adding the below properties

https://sap.github.io/cloud-commerce-spartacus-storefront-docs/installing-sap-commerce-cloud/#config...

But getting the same issue now in my cloud

Accepted Solutions (0)

Answers (4)

Answers (4)

app.module.ts file

If you face the issue with 2005 Commerce Cloud version. Please try by replacing the rest with occ as in version 2005 ycommercewebservices extension is replaced with commercewebservices extesnion which has webroot as /occ where the latter has /rest.

former_member624092
Participant
0 Kudos

If you still have issues with your CORS settings on your backend, you can all the time use proxying-to-a-backend-serve in your dev ENV. This will proxy the call to your backend.
Here is an example of proxy.conf.json to proxy /api to a free api out there

{
  "/api": {
    "target": "https://api.openbrewerydb.org",
    "pathRewrite": {
      "^/api": ""
    },
    "secure": false,
    "changeOrigin": true,
    "logLevel": "debug"
  }
}


former_member624092
Participant
0 Kudos

Try using the Spartacus Demo API by initialising your angular

    B2cStorefrontModule.withConfig({
      backend: {
        occ: {
          baseUrl:
            'https://storefront.c39j2-walkersde1-d4-public.model-t.cc.commerce.ondemand.com',
        },
      },
      context: {
        urlParameters: ['baseSite', 'language', 'currency'],
        baseSite: ['electronics-spa'],
      },

If this works, then there might be an issue on how your backend responds. You can try to use something like cors-anywhere for debugging, but please don't use something like this in production.

former_member666997
Discoverer
0 Kudos

I also encountered that error while following the Spartacus documentation. In my case, I was creating the Angular Project with schematics and that error showed up, then by switching into downloading the dependencies the Storefront run with no issues.

0 Kudos

Are you getting this error in your local or in the cloud? I am facing the same issue in my cloud. I tried changing the localhost with correct hostname, still facing the same issue.