Uncaught Error: Incompatible SockJS! - Angular 2 CLI

假装没事ソ 提交于 2019-12-12 19:18:01

问题


I have been getting the error:

'Uncaught Error: Incompatibile SockJS! Main site uses: "1.1.1", the iframe: "1.0.1".'

I was not getting this error until I added the following code to a service:

*COMPONENT*
this.dataService.getData(1,null).subscribe(
    result => this.allSCATSsites = result.json(),
    () => console.log("Failed..."),
    () => this.processSites()
);

*SERVICE*
getData(functionindex: number, dataarray) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http.post(url, sentArray,{
        headers: headers
    });
}

The weird problem is that I don't get the above error on every execution. it appears random.

I have no idea where to look or how to debug this - or if this even has anything to do with the Angular 2 CLI. Research indicates something to do with Webpack dependencies within the Angular 2 CLI but I can't seem to pinpoint it - my package.json file doesn't have Webpack (or SockJS) within it, but Webpack is within my Node Modules folder.

Any ideas how to solve this or even where I should be looking? I have tried upgrading to the latest version of the Angular 2 CLI and migrating the project to the new version also.


回答1:


One reason for this error may be the following (however, this would not account for the fact that your problem only occurred after adding the code mentioned above, and you didn't mention that you are using Websockets - so this may not solve your problem, but hopefully that of others):

If you have your requests routed through an HTTP server before they arrive at the application server, you have to make sure that the HTTP server lets the Websockets requests pass correctly.

When using Apache HTTP server, you could configure this like that (assuming that /myapp/ws is your websockets endpoint):

ProxyPassMatch ^/myapp/ws/(.*)/websocket$ ws://localhost:8080/myapp/ws/$1/websocket

For this to work, you must make sure that the proxy_wstunnel module is installed and enabled:

sudo apt-get update
sudo apt install apache2

sudo a2enmod proxy_http
sudo a2enmod proxy_wstunnel

Note: If you haven't installed that correctly, then Spring Websockets will try a fallback to other options. In this case, an IFrame will be added which again loads a different socksjs library than that that you use in your application. And this will cause the error mentioned above.



来源:https://stackoverflow.com/questions/41951006/uncaught-error-incompatible-sockjs-angular-2-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!