鍍金池/ 問答/HTML/ koa接口跨域報錯

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'],
}))
//省略封裝的函數(shù)代碼
app.use(router['routes']());
router.get('/',findFn)
router.get('/add',insertFn)
app.listen(3000);

接口截圖

請求頁面截圖

接口請求頭信息

讓我的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.)

望大家指點一二,謝謝

回答
編輯回答
陌南塵

response上看不是后端代碼的問題,貼一下前端代碼,錯誤信息說only supported for protocol schemes:http, data, chrome, chrome-extension, https,懷疑是你scheme寫錯。

2017年8月24日 14:26
編輯回答
拽很帥

你的錯誤不是跨域的錯誤,看起來是你直接本地打開發(fā)的請求,起個服務(wù)打開試試

2017年10月6日 13:49
編輯回答
拽很帥

已解決 請求的地址忘記加http:// 真是尷尬

2018年9月18日 19:53