several interfaces are written with koa+mongodb. Cross-domain errors are always reported when requesting with ajax, even if cors is used in koa. Google has done a lot of things but still hasn"t solved the problem
.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);
Let the interface that requests localhost:3000 in my localhost:1234/index.html page report the error (Failed to load localhost:3000: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. shown above)
I hope you can give me some advice. Thank you