用koa+mongodb寫(xiě)了幾個(gè)接口,用ajax請(qǐng)求時(shí)總是跨域報(bào)錯(cuò),koa中用了cors也不行,谷歌了好多還是沒(méi)有解決問(wèn)題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 "*"; // 允許來(lái)自所有域名請(qǐng)求 }, exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'], maxAge: 5, credentials: true, allowMethods: ['GET', 'POST', 'DELETE'], allowHeaders: ['Content-Type', 'Authorization', 'Accept'],}))//省略封裝的函數(shù)代碼app.use(router['routes']());router.get('/',findFn)router.get('/add',insertFn)app.listen(3000);讓我的localhost:1234/index.html頁(yè)面里請(qǐng)求localhost:3000的接口就會(huì)報(bào)如上圖的錯(cuò)誤(Failed to load localhost:3000: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.)望大家指點(diǎn)一二,謝謝
koa接口跨域報(bào)錯(cuò)
至尊寶的傳說(shuō)
2019-03-15 18:14:30