逐浪云主机

立即开通

Enable Access-Control-Allow-Origin in IIS6 & IIS7 for webapi calls 配置IIS站点JS支持跨域访问

作者:本站编辑 发布时间:2015-05-20 来源:本站原创 点击数:

在做webfont引用时,总是不能成功调用,不得期解,事后才知道是JS不能跨域造成。


引起了ajax不能跨域请求的问题?
ajax本身实际上是通过XMLHttpRequest对象来进行数据的交互,而浏览器出于安全考虑,不允许js代码进行跨域操作,所以会警告。 
有什么完美的解决方案么? 
解决方案有不少,但是只能是根据自己的实际情况来选择。 
跨域的安全限制都是指浏览器端来说的,服务器端是不存在跨域安全限制的。所以针对这2种情况衍生出2类跨域解决方案,一类是服务器端做中转类似代理方式,一类是js处理浏览器端的真正跨域访问。 
具体情况有: 
1) 本域和子域的相互访问: www.aa.com和book.aa.com 用 document.domain =
"aa.com";
2) 本域和其他域的相互访问: www.aa.comwww.bb.com 用 XMLHttpRequest访问代
理,既服务器端代理方式
3) 本域和其他域的相互访问: www.aa.comwww.bb.com 用 JS创建动态脚本,<script>
标签的src属性实现跨域访问


在Chrome中错误提示:

Font from origin 'http://code.zoomla.cn' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

To allow your newly created webapi web service calls to be accessed from ajax post calls from another website you need to enable this setting in either IIS6 or IIS7

IIS6

  1. Open Internet Information Service (IIS) Manager

  2. Right click the site you want to enable CORS for and go to Properties

  3. Change to the HTTP Headers tab

  4. In the Custom HTTP headers section, click Add

  5. Enter Access-Control-Allow-Origin as the header name

  6. Enter * as the header value

  7. Click Ok twice

IIS7 – Add this to your web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>

 

本文责任编辑: 加入会员收藏夹 点此参与评论>>
复制本网址-发给QQ/微信上的朋友