H
H
hollanditkzn2017-08-24 15:52:12
JavaScript
hollanditkzn, 2017-08-24 15:52:12

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.

The file itself looks like this
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'});
});

I also tried to do it in the template, but I got an error
Something like this
file_get_contents(NodeSocketAssets::register($this);)

curl_init(NodeSocketAssets::register($this);)
header('Access-Control-Allow-Origin: http://localhost:3001/socket.io/1/?t=1503580570034')

Of course, this is wrong, but without a concept as a headline and where to put it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura Komarov, 2017-08-25
@Yurajun

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 question

Ask a Question

731 491 924 answers to any question