javascript - 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
解决方案
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);
});
}
推荐阅读
- python - Pycharm 对嵌套类属性缺乏 Intelisense
- react-google-maps - react-google-maps StandaloneSearchBox 设置特定的国家限制?
- java - 从结果集中创建 CSV 块
- xcode10 - 如何在 Xcode 10 中停靠对象库
- javascript - Perl DateTime 纪元是以毫秒还是秒为单位?
- python - 如何使用 structlog 将日期和日期时间记录为字符串?
- mysql - 高效地将 WTForm 提交写入 mysql
- c# - 预热后缩放后请求很慢
- windows - bash 脚本中的变量被视为命令 - “找不到命令”
- ruby-on-rails - 使用 Ruby 从字符串中删除字节大小大于 2 的字符