跨域(Cross-Domain) AJAX for IE8 and IE9

时间:2022-08-28 15:31:33

1、有过这样一段代码,是ajax

$.ajax({
        url: "http://127.0.0.1:9001",
        type: "POST",
        data: JSON.stringify({"reqMsg":"12345"}),
        dataType: 'json',
        timeout: 1000 * 30,
        success: function (response) {
            if(response.n=='6'){
                dosomething();
                init();
            }else if(response.n=='3'){
                init();
            }
        },
        error: function(xhr, type){
            init();
        }
    });

2、在谷歌、IE11等浏览器测试没有问题,但是碰巧客户现场是IE8,9这种浏览器,内网系统没办法升级。出现跨域ajax请求插件直接error的情况

3、解决方案在github上找到了解决办法,https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest    这个路径下表述了如何解决Cross-Domain AJAX for IE8 and IE9此类问题。

4、首先需要再页面引入 jquery.xdomainrequest.min.js 

<script type="text/javascript" src="<%=basePath%>resources/js/jquery.min-1.9.1.js"></script>
<script type="text/javascript" src="<%=basePath%>resources/js/jquery.xdomainrequest.min.js"></script>

5、然后再正常写ajax之前,加上一句jQuery.support.cors = true;

jQuery.support.cors = true;
$.ajax({
        url: "http://127.0.0.1:9001",
        type: "POST",
        data: JSON.stringify({"reqMsg":"12345"}),
        dataType: 'json',
        timeout: 1000 * 30,
        success: function (response) {
            if(response.n=='6'){
                dosomething();
                init();
            }else if(response.n=='3'){
                init();
            }
        },
        error: function(xhr, type){
            init();
        }
    });

6、这样就解决了。最后附上jquery.xdomainrequest.min.js 源码

/*!
 * jQuery-ajaxTransport-XDomainRequest - v1.0.3 - 2014-06-06
 * https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest
 * Copyright (c) 2014 Jason Moon (@JSONMOON)
 * Licensed MIT (/blob/master/LICENSE.txt)
 */
(function(a){if(typeof define==='function'&&define.amd){define(['jquery'],a)}else if(typeof exports==='object'){module.exports=a(require('jquery'))}else{a(jQuery)}}(function($){if($.support.cors||!$.ajaxTransport||!window.XDomainRequest){return}var n=/^https?:\/\//i;var o=/^get|post$/i;var p=new RegExp('^'+location.protocol,'i');$.ajaxTransport('* text html xml json',function(j,k,l){if(!j.crossDomain||!j.async||!o.test(j.type)||!n.test(j.url)||!p.test(j.url)){return}var m=null;return{send:function(f,g){var h='';var i=(k.dataType||'').toLowerCase();m=new XDomainRequest();if(/^\d+$/.test(k.timeout)){m.timeout=k.timeout}m.ontimeout=function(){g(500,'timeout')};m.onload=function(){var a='Content-Length: '+m.responseText.length+'\r\nContent-Type: '+m.contentType;var b={code:200,message:'success'};var c={text:m.responseText};try{if(i==='html'||/text\/html/i.test(m.contentType)){c.html=m.responseText}else if(i==='json'||(i!=='text'&&/\/json/i.test(m.contentType))){try{c.json=$.parseJSON(m.responseText)}catch(e){b.code=500;b.message='parseerror'}}else if(i==='xml'||(i!=='text'&&/\/xml/i.test(m.contentType))){var d=new ActiveXObject('Microsoft.XMLDOM');d.async=false;try{d.loadXML(m.responseText)}catch(e){d=undefined}if(!d||!d.documentElement||d.getElementsByTagName('parsererror').length){b.code=500;b.message='parseerror';throw'Invalid XML: '+m.responseText;}c.xml=d}}catch(parseMessage){throw parseMessage;}finally{g(b.code,b.message,c,a)}};m.onprogress=function(){};m.onerror=function(){g(500,'error',{text:m.responseText})};if(k.data){h=($.type(k.data)==='string')?k.data:$.param(k.data)}m.open(j.type,j.url);m.send(h)},abort:function(){if(m){m.abort()}}}})}));