Home » Java » java – CORS Error (Access-Control-Allow-Origin) while connecting websockets (Sockjs, stomp-websocket)-Exceptionshub

java – CORS Error (Access-Control-Allow-Origin) while connecting websockets (Sockjs, stomp-websocket)-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m try to open sockets in my React App. I am using Java (Spring ) for back-end and React for front-end.

in ComponentDidMount I added the Following code, which I copied from Spring Documentation.

    componentDidMount () {

    let socket = new SockJS('http://172.16.10.154:8099/ws');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        console.log('Connected: ' + frame);
        stompClient.subscribe('/queue/notify/1152923083608410997', function (greeting) {
            console.log("Geeting is ",greeting)
        });
    });
}

now when ComponentDidMount is call, I get the following error in console
Console Error

I tried the exact same code in a static html file, where I put this code in and call this on a button Click. The strange thing is that it’s working in that file.
What could be the possible reason for this problem? Is it from back-end?

How to&Answers: