【问题标题】:Simple CORS setup not working简单的 CORS 设置不起作用
【发布时间】:2018-03-15 09:29:40
【问题描述】:

这是我的服务器代码:

import express from 'express';
import cors from 'cors';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';

const app = express();

// app.use(cors()); // Also tried this
app.use(cors({ credentials: true, origin: true }));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.get('/', (req, res) => res.json({ message: 'OK' }));

app.listen(3000);

这是我的网站代码(来自localhost:5000):

const response = await superagent.get('localhost:3000/')
  .withCredentials()
  .send();

但这被CORS阻止了:

错误:请求已终止 可能原因:网络离线、Access-Control-Allow-Origin不允许Origin、正在卸载页面等

为什么?


我使用的是 Firefox 58 和 Node.js 6.10。

【问题讨论】:

    标签: node.js express superagent


    【解决方案1】:

    我忘记了http:// 前缀:

    const response = await superagent.get('http://localhost:3000/')
      .send();
    

    多么美好的一天……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      相关资源
      最近更新 更多