[英]CORS with Express.js and Angular2
我正在嘗試從遠程Express.js服務器將PLY文件下載到Angular / Ionic應用程序。 我現在將Ionic應用程序托管在Amazon AWS上。 這是Ionic應用程序中的打字稿:
//this.currentPlyFile encompasses entire URL
document.getElementById('entity').setAttribute("ply-model", "src: url(" + this.currentPlyFile + ".ply);");
我的Express.js服務器中包含以下內容:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST');
res.header('Access-Control-Allow-Headers', 'appid, X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
});
但是當請求PLY文件時出現以下錯誤:
XMLHttpRequest cannot load "my url here" No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'my url here' is therefore not allowed access.
這確實令人沮喪,因為我使用Express.js文檔提供的標頭來允許CORS。
預檢->選項-> https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS- > next()
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.header('Access-Control-Allow-Headers', 'appid, X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
next();
});
app.get('/', function (req, res) {
res.send('OK');
});
注意:將這些內容移至configure函數的頂部。
或簡單使用表達cors :
var express = require('express')
var cors = require('cors')
var app = express();
app.use(cors());
app.get('/', function (req, res) {
res.send('OK');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.