Answer the question
In order to leave comments, you need to log in
No 'Access-Control-Allow-Origin' header is present on the requested resource. How to fix the situation?
I use Ionic, Socket.io in the project. The backend is written in node.js.
I'm trying to reach a server located in the Cloud9 cloud using the client.
Gives an error:
XMLHttpRequest cannot load https:/URL. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' 192.168.10.159:8100 ' is therefore not allowed access.
As I understand it, the problem is security, that Chrome does not allow access to an external resource.
Tried:
1) "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files --disable-web-security
2) ionic project
"proxies": {
"path": "/chat",
"proxyUrl": "
}
3) I tried a plugin that disables/enables CORS for Chrome.
Everywhere is the same. Does not help
I contact the server through the service:
angular.module('starter')
.factory('Socket', function (socketFactory) {
var url = 'https:...';
var myIoSocket = io.connect( url );
mySocket = socketFactory({
ioSocket: myIoSocket
});
return mySocket;
});
// Including Express.js
var express = require('express');
// Creating app Express
var app = express();
// Creating server using express and http
var server = require('http').createServer(app);
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
server.on('request', function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/event-stream; charset=utf-8',
'Cache-Control': 'no-cache',
'Access-Control-Allow-Origin': '*'
});
res.end();
})
Answer the question
In order to leave comments, you need to log in
I recently tested the EventSource and accessed cloud9 through angular2. Had the same problem.
I solved by setting Access-Control-Allow-Origin on the server via
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
// 'Access-Control-Allow-Credentials': true
});
var express = require('express'),
cors = require('cors'),
app = express();
app.use(cors());
// или
app.use(cors({
origin: true,
credentials: true
}));
1) ionic proxy should work
2) Chrome launched with the flag should work (but it needs to be launched from scratch - if Chrome is already open, it will just pop into the foreground, without applying the flag, of course)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question