ie9下ajax拒绝访问,带有CORS的IE9 jQuery AJAX返回“访问被拒绝”

该博客介绍了如何解决IE9及以下版本不支持CORS的问题。通过创建一个jQuery脚本,当检测到跨域请求且浏览器为IE9或更低版本时,自动使用代理服务器进行请求。代理服务器会读取X-CorsProxy-Url头并转发请求,从而实现所有HTTP方法和JSON等类型的支持,无需修改原有ASP.NET代码。
摘要由CSDN通过智能技术生成

The problem is that IE9 and below do not support CORS. XDomainRequest do only support GET/POST and the text/plain conten-type as described here: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

So if you want to use all HTTP verbs and/or json etc you have to use another solution. I've written a proxy which will gracefully downgrade to proxying if IE9 or less is used. You do not have to change your code at all if you are using ASP.NET.

The solution is in two parts. The first one is a jquery script which hooks into the jQuery ajax processing. It will automatically call the webserver if an crossDomain request is made and the browser is IE:

$.ajaxPrefilter(function (options, originalOptions, jqXhr) {

if (!window.CorsProxyUrl) {

window.CorsProxyUrl = '/corsproxy/';

}

// only proxy those requests

// that are marked as crossDomain requests.

if (!options.crossDomain) {

return;

}

if (getIeVersion() && getIeVersion() < 10) {

var url = options.url;

options.beforeSend = function (request) {

request.setRequestHeader("X-CorsProxy-Url", url);

};

options.url = window.CorsProxyUrl;

options.crossDomain = false;

}

});

In your web server you have to receive the request, get the value from the X-CorsProxy-Url http header and do a HTTP request and finally return the result.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值