之前在NodeJs寫了一個API 來方便自己去取資料..
誰不知當我在其他網頁用這個API 時出現了”Access-Control-Allow-Origin header” 的問題..
“Origin file: not found in Access-Control-Allow-Origin header”
做了一會Research後 找到了解決方法了
我們只需要加入以下的”allowCrossDomain” 功能 便可以了
解決方法
var express = require('express'); var app = express(); // Code to solve the Access-Control-Allow-Origin header Issue var allowCrossDomain = function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'); // intercept OPTIONS method if ('OPTIONS' == req.method) { res.send(200); } else { next(); } }; app.use(allowCrossDomain);
Hope you find it useful