2016-07-21 66 views
0

我們的門戶網站需要跨域工作。當前版本的代碼在IE中工作(使用明確的HTML設置來模擬IE 7)。相同的代碼在Chrome或Mozilla中無法使用。點擊鏈接時會彈出一個錯誤提示。它讀取 - 「發生錯誤:0錯誤文件名= myfile.html」跨域設置在IE10不工作 - 工作在瀏覽器和Mozilla

我修改的JavaScript代碼,如下所示。

//code for getting response from cross domain 
$.ajaxPrefilter(function (options) { 
    if (options.crossDomain && jQuery.support.cors) { 
    var http = (window.location.protocol === 'http:' ? 'http:' : 'https:'); 
    options.url = http + '//cors-anywhere.herokuapp.com/' + options.url; 
    //options.url = "http://cors.corsproxy.io/url=" + options.url; 
    } 
}); 
$.get(
    'http://www.myportal.com/sierra/xxxx.html', 
    function (response) { 
     console.log("> ", response); 
     $("#viewer").html(response); 
}); 

此代碼在Chrome和Mozilla中正常工作,但在IE中無法使用。然後,我刪除了模擬IE7的HTML標記,並用邊緣代替它。即使這樣也行不通。它只在幾個安全設置被禁用後纔開始工作。但最終用戶不希望僅更改任何瀏覽器設置才能訪問我的網站。

是否有任何其他方式可以在所有瀏覽器上進行跨域訪問?

+0

能否請您如果可能分享我們的jsfiddle鏈接? –

回答

0

代替$獲得(),使用這段JavaScript代碼:

function newReq(url,callBack) 
{ 
var xmlhttp; 
if (window.XDomainRequest) 
{ 
    xmlhttp=new XDomainRequest(); 
    xmlhttp.onload = function(){callBack(xmlhttp.responseText)}; 
} 
else if (window.XMLHttpRequest) 
    xmlhttp=new XMLHttpRequest(); 
else 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
xmlhttp.onreadystatechange=function() 
{ 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     callBack(xmlhttp.responseText); 
} 
xmlhttp.open("GET",url,true); 
xmlhttp.send(); 
} 
相關問題