我的项目在带有 Express 后端的 Node 上运行。
我正在尝试使用 Arangojs 查询我的 Arango 数据库客户端。 ArangoDB 在 Digital Ocean 上的 Docker 上运行。我在查询我的数据库服务器端没有问题,但是我在页面加载时收到以下错误:
Failed to load http://0.0.0.0:8529/_db/database/_api/cursor: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is 'false' which must be 'true' when the request's credentials mode is 'include'. Origin 'http://localhost:3000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
我在客户端 js 上的代码如下所示:
var db = new arangojs.Database({url:'http://0.0.0.0:8529'})
db.useDatabase(dbase)
db.useBasicAuth("database", 'password')
db.query('FOR doc IN docs RETURN doc') // etc. etc.
编辑: 1 年后事后看来,这个问题非常愚蠢 - 正确答案是不要通过客户端 JS 公开您的数据库凭据...与您的后端通信,并让与您的数据存储区通信。
最佳答案
您正在配置 cors()
错误,您必须使用 credentials
属性才能配置 Access-Control-Allow-Credentials:
var cors = require('cors');
var corsOptions = {
origin: '*',
credentials: true };
app.use(cors(corsOptions));
除此之外,您的 app.all(* ...
不是必需的,因为 app.use(cors(corsOptions));
已经为您处理了。
关于javascript - CORS 'Allow-Credentials' Node/ express ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49189058/