javascript - CORS 'Allow-Credentials' Node/ express

标签 javascript node.js express cors xmlhttprequest

我的项目在带有 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/

相关文章:

javascript - 无法使用 mongoose、Node.js 将数据插入 mongoDB 中的文档字段

node.js - 如何使用 Node/Express 运行纯前端 Angular 应用程序?

javascript - 如何突出显示我的文字

javascript - Promise.catch 正在吞噬错误

javascript - Ignite UI 树网格未使用新数据更新表

angularjs - 如何从 url 中删除 # 标签 MEAN STACK

javascript - 无法读取express.js中上传的图像

node.js - 当我尝试运行此代码时出现以下错误

javascript - Bootstrap 日期选择器 endDate

javascript - 在 Laravel 中编辑登录路径以更改语言首选项