koa接口跨域报错

用koa+mongodb写了几个接口,用ajax请求时总是跨域报错,koa中用了cors也不行,谷歌了好多还是没有解决问题

const cors = require('koa2-cors');

var MongoClient = require("mongodb").MongoClient;

var DB_URL = "mongodb://localhost:27017/test";

// app.use(cors());

app.use(cors({

    origin: function (ctx) {

            return "*"; // 允许来自所有域名请求

    },

    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],

    maxAge: 5,

    credentials: true,

    allowMethods: ['GET', 'POST', 'DELETE'],

    allowHeaders: ['Content-Type', 'Authorization', 'Accept'],

}))

//省略封装的函数代码

app.use(router['routes']());

router.get('/',findFn)

router.get('/add',insertFn)

app.listen(3000);

https://img3.mukewang.com/5c987f780001fa1a05580728.jpg

https://img4.mukewang.com/5c987f740001e38e08000198.jpg

https://img4.mukewang.com/5c987f7500016df608000352.jpg


让我的localhost:1234/index.html页面里请求localhost:3000的接口就会报如上图的错误(Failed to load localhost:3000: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.)

望大家指点一二,谢谢


https://img.mukewang.com/5c987f730001fa1a05580728.jpg


至尊宝的传说
浏览 898回答 3
3回答

青春有我

你的错误不是跨域的错误,看起来是你直接本地打开发的请求,起个服务打开试试

慕村225694

从response上看不是后端代码的问题,贴一下前端代码,错误信息说only supported for protocol schemes:http, data, chrome, chrome-extension, https,怀疑是你scheme写错。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript