C#WebService 出现Access-Control-Allow-Origin header is present on the requested resource无响应解决方法

来源:互联网 发布:阿里云 cdn 带宽限制 编辑:程序博客网 时间:2024/05/22 09:01

今天前端突然说调用我写的webservice挂掉了报错,一看错误信息是跨域的问题:

【XMLHttpRequest cannot load http://localhost:8018/WebService.asmx?op=UserLoginNo 'Access-Control-Allow-Origin' header is present on the requested resource. Origin http://localhost:8018/WebService.asmx?op=UserLogin' is therefore not allowed access】


然后检查了下,首先排除代码,最近没更新过,那就是服务器里面的设置的问题了,前几天服务器刚挂掉,客户重新安装的,检查下发现远程服务器的访问IP和 里面的本地IP不一致的


解决方法:

第一反应:由于是在JS里面调用的就   dataType: 'json', 改成dataType: 'jsonp',  但这样工作量会变大,因为JS里面的调用都的改

第二种方法:

在config里面加上

<system.webServer><httpProtocol><customHeaders><add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/><add name="Access-Control-Allow-Headers" value="x-requested-with"/><add name="Access-Control-Allow-Origin" value="*" />//表示允许所有来源进行跨域访问</customHeaders></httpProtocol></system.webServer>
其他参考地址:

http://www.cnblogs.com/yangecnu/p/introduce-cross-domain.html

http://blog.csdn.net/qq_32786873/article/details/52170229

阅读全文
0 0