![](/img/trans.png)
[英]ionic: No 'Access-Control-Allow-Origin' header is present on the requested resource
[英]Meteor Mobile Server on Heroku - No Access-Control-Allow-Origin header is present
首先,我說我在網上找到了幾個建議的解決方案,但是似乎沒有一個適合我。
問題:
我有一個流星應用程序,我試圖在android上運行。 為此,我將應用程序部署在Heroku上,並使用--mobile-server https://myapp.heroku.com
參數調用run android-device
命令。
我永久收到錯誤
"XMLHttpRequest cannot load https://myapp.heroku.com/sockjs/... . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:12848' is therefore not allowed access. The response had HTTP status code 404.", source: http://localhost:12848/ (0)
到目前為止,這是我嘗試過的:
我在流星啟動時設置了ROOT URL:
process.env.ROOT_URL = "https://myapp.heroku.com";
我嘗試在流星啟動時在服務器端這樣設置訪問控制:
WebApp.connectHandlers.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header('Access-Control-Allow-Origin', 'https://myapp.heroku.com');
res.header('Access-Control-Allow-Origin', 'http://localhost:12848');
res.header('Access-Control-Allow-Origin', 'http://meteor.local');
res.header("Access-Control-Allow-Headers", "Content-type,Accept,X-Custom-Header");
return next();
});
我試圖在流星啟動時在服務器端使用這種瀏覽器策略包:
BrowserPolicy.content.allowSameOriginForAll();
BrowserPolicy.content.allowOriginForAll('*');
BrowserPolicy.content.allowOriginForAll('http://meteor.local');
BrowserPolicy.content.allowOriginForAll('https://myapp.heroku.com');
BrowserPolicy.content.allowOriginForAll('https://*.myapp.heroku.com');
BrowserPolicy.content.allowEval();
我嘗試將訪問規則添加到“ mobile-config.js”:
App.accessRule("*");
我確保根目錄下“ package.json”文件中的名稱與“ mobile-config.js”下的應用程序名稱相同
我還想念什么?
編輯:
我也嘗試過將express和cors軟件包添加到本地主機白名單中:
var whitelist = [
'http://localhost:3000',
'http://localhost:12848',
'https://myapp.heroku.com'
];
var corsOptions = {
origin: function(origin, callback){
var originIsWhitelisted = whitelist.indexOf(origin) !== -1;
callback(null, originIsWhitelisted);
},
credentials: true
};
app.use(cors(corsOptions));
還嘗試啟用預檢,如下所示:
app.options('*', cors())
這可能是我遇到過的最愚蠢的問題。 嘗試使用--mobile-server https://myapp.heroku.com
參數運行該應用程序是錯誤的。 相反,它應該是https://myapp.herokuapp.com
就是這樣 一直就是問題所在...
在白名單中添加“ *”即可完成此工作。 最終解決方案位於config.xml中,這應該會有所幫助: https : //stackoverflow.com/a/36124935/8056323
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.