javascript - Node.js 中的 CORS 问题失败

标签 javascript node.js cors cross-domain

app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8100');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', '*');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
});

我使用 postman 一切工作正常,但在测试我的 ionic 应用程序时遇到了 CORS 问题,该应用程序运行于 http://localhost:8100 。我用谷歌搜索并设法找到上述设置 header 解决方案,但现在我收到此错误:

Request header field owner is not allowed by Access-Control-Allow-Headers in preflight response.

有什么想法吗?

最佳答案

尝试使用 cors 中间件。

var cors = require('cors');

app.use(cors());

关于javascript - Node.js 中的 CORS 问题失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35953286/

相关文章:

Javascript .replace() 在 Chrome 中全局不起作用

javascript - Chrome 扩展程序 : Content of localStorage is not visible in Chrome DevTools

node.js - 如何使用node.js检索特定ewelink设备的数据?

javascript - 为什么下面的setTimeout直接打印出来了?

javascript - 当 meteor 应用程序运行时,有哪些方法可以在服务器端执行任意代码?

javascript - 在 node.js 模块中将原型(prototype)函数添加到 JSON

javascript - 表单输入历史选择事件

java - Spring Boot CORS设置: CrossOrigin annotation works addCorsMappings doesn't

javascript - 为什么 jQuery 在我的 Ajax 请求中包含 Origin header 以及如何从中删除它?

http-headers - 如何在 Apache Wink 中处理 CORS?