首页 > 解决方案 > javascript signalR client : CORS policy error trying to connect to api

问题描述

I'm a php/js developer and have no knowledge of c# /asp, trying to talk to signalR API for the first time

i tried using this client library from official signalR page

https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client

here is my code

<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.signalr-2.2.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {

        $.connection('https://firouzex.exphoenixtrade.com/realtime').start()
            .done(function(){
                 console.log('connection done!')
            })
            .fail(function(){
                console.log('error!');
            });


     });
</script>

but i got

Access to XMLHttpRequest at 'https://firouzex.exphoenixtrade.com/realtime/negotiate?clientProtocol=1.5&_=1560862133617' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

标签: javascriptc#asp.netnode.jssignalr

解决方案


This seems to be an issue on the server side on how the Signalr is being setup. If you have access to that, can you try?

        public void Configuration(IAppBuilder app)
        {                
                app.Map("/signalr", map =>
                {
                    map.UseCors(CorsOptions.AllowAll);
                    var hubConfiguration = new HubConfiguration { };
                    map.RunSignalR(hubConfiguration);
                });
         }

推荐阅读