2017-03-05 3 views
1

リモートExpress.jsサーバーからAngular/IonicアプリにPLYファイルをダウンロードしようとしています。 Amazon AWSで私のイオニックアプリを今すぐホストしています。私は私のExpress.jsサーバに以下のいるCORS with Express.jsとAngular2

//this.currentPlyFile encompasses entire URL 
document.getElementById('entity').setAttribute("ply-model", "src: url(" + this.currentPlyFile + ".ply);"); 

:ここで活字体は、イオンアプリからだ

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. 

CORSを許可するExpress.jsのドキュメントで提供されているヘッダーを使用しているので、これは本当にイライラしています。

答えて

5

プリフライト - >オプション - >https://developer.mozilla.org/en-US/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関数の先頭にその原料を移動します。


や簡単な使用express cors

var express = require('express') 
var cors = require('cors') 
var app = express(); 

app.use(cors()); 

app.get('/', function (req, res) { 
    res.send('OK'); 
});