Answer the question
In order to leave comments, you need to log in
How to allow Access-Control-Allow-Origin access?
The yii2 project itself, connected the yii2-node widget , now the project needs web sockets, as the load is constantly increasing and we still need to implement socket notification. Maybe on yii2 someone came across this widget, so I chose additionally php yii2. When I connected the js file with the settings, the error fell
GET localhost:3001/socket.io/1/?t=1503578120929 403 (Forbidden)
XMLHttpRequest cannot load localhost:3001/socket.io/1/?t=1503578120929 . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' crm ' is therefore not allowed access. The response had HTTP status code 403.
var socket = new YiiNodeSocket();
socket.debug(true);
socket.onConnect(function () {
socket.room('testRoom').join(function (success, numberOfRoomSubscribers) {
if(success){
console.log(numberOfRoomSubscribers + ' clients in room: '+roomId);
// do something
// bind events
this.on('join', function (newMembersCount) {
// fire on client join
});
this.on('data', function (data) {
// fire when server send frame into this room with 'data' event
});
} else {
// numberOfRoomSubscribers - error message
alert(numberOfRoomSubscribers);
}
})
});
//По идеи так должен же работать заголовок?
socket.on('request', function (req, res) {
res.writeHead(200, {
'Access-Control-Allow-Origin': 'http://localhost:3001/socket.io/1/?t=1503580570034'});
});
file_get_contents(NodeSocketAssets::register($this);)
curl_init(NodeSocketAssets::register($this);)
header('Access-Control-Allow-Origin: http://localhost:3001/socket.io/1/?t=1503580570034')
Answer the question
In order to leave comments, you need to log in
It is necessary to install socket in the system. I do not remember what, I had such a mistake. I installed via npm a package with socket like and everything was ok.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question