Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.07k stars 1.19k forks source link

@azure/cosmos not working with CORS #6576

Closed middiu closed 4 years ago

middiu commented 4 years ago

Describe the bug No matter the CORS settings I put in Azure portal I keep getting the error message "Access to fetch at 'https://xyz.documents.azure.com/' from origin 'https://localhost:3000' has been blocked by CORS policy"

To Reproduce Steps to reproduce the behavior:

  1. Created a react app using create-react-app, install @azure/cosmos module and try to connect to a test dabatabase/container

Expected behavior I should be able to access cosmos db if https://localhost:3000 is in the CORS hosts list in Azure portal or if that setting is set to * Screenshots If applicable, add screenshots to help explain your problem. image

image

This is my code:

const { CosmosClient } = require("@azure/cosmos");
...
...
  constructor() {
    this._dbclient = new CosmosClient({ endpoint, key },);
  }
...
...
...
    const dbResponse = await this._dbclient.databases.createIfNotExists({
      id: DatabaseId
    });
    this._db = dbResponse.database;
    debug("Setting up the database...done!");
    debug("Setting up the container...");
    const coResponse = await this._db.containers.createIfNotExists({
      id: ContainerId
    });
    this._container = coResponse.container;
    debug("Setting up the container...done!");
  }

any help or suggestion would be really appreciated.

Additional context Add any other context about the problem here.

southpolesteve commented 4 years ago

@middiu Can you try it with endpoint discovery turned off?

const client = new CosmosClient({
  endpoint,
  key,
  connectionPolicy: {
    enableEndpointDiscovery: false
  }
})

There is a known bug around CORS and the endpoint discovery call which we are actively working to fix. ETA Feb 2020

middiu commented 4 years ago

too easy. Thanks @southpolesteve, this fixes the issue.

Thanks a lot

southpolesteve commented 4 years ago

@middiu Glad it worked. I am going to reopen this in case other people run into it and will close it later

southpolesteve commented 4 years ago

A backend change has been deployed that fixes this issue. Using enableEndpointDiscovery: false is no longer necessary to support CORS

udithait commented 3 years ago

still having the issue for localhost bb

roblykens commented 2 years ago

I found this to still be an issue. I have a module that is called with helper classes to call the api (managed) and the api call the cosmos db to fetch data and bring it back to ultimately be used by the UI,

index>module>helpermodule>api>cosmos

I added the option to the code as noted above : connectionPolicy: { enableEndpointDiscovery: false } and it worked properly to get the data, otherwise it gave a 500 and the underlying cause is CORS.