2013-03-25 104 views
0

我無法在IE9上使用此腳本訪問URL。在Chrome和Firefox上,它工作正常。當我調試在IE9(F12),我得到:SCRIPT5:訪問在IE9上被拒絕

SCRIPT5: ACCESS DENIED.

我的功能:

function NewPage2() { 
    var xmlHttp; 
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlHttp = new XMLHttpRequest(); 

    } else { // code for IE6, IE5 
     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlHttp.open("GET", "https://graph.facebook.com/oauth/access_token?client_id=" + 
     '<%=ConfigurationManager.AppSettings["clientId"].ToString() %>' + 
     '&redirect_uri=' + <%=ConfigurationManager.AppSettings["redirectUrl"].ToString() %>' + 
     '&state=' + document.getElementById('text').value + 
     '&client_secret=' + '<%=ConfigurationManager.AppSettings["client_secret"].ToString() %>' + 
     '&code=' + getUrlVars2()["code"], false); 
    xmlHttp.send(null); 
    end(xmlHttp.responseText + "&userId=" + getUrlVars2()["state"]); 
} 

回答

1

發生這種情況,當你嘗試從不同的域訪問一個Ajax請求你的主頁。 (在這種情況下,您正在訪問來自Facebook的URL)。

如果您需要訪問來自不同域的URL,則稱爲跨站點請求。由於安全性的影響,這些默認情況下會被阻止,但仍然可以通過多做一些工作來完成。

使用jQuery很容易。

奇怪的是,儘管你在問題的標籤中包含了jQuery,但你的實際代碼根本不使用jQuery--事實上,你提供的所有代碼都是單行的jQuery,加上它可以處理跨站點請求。

您需要使用一個Ajax技術的呼叫JSONP,你可以在jQuery的網站在這裏發現了記載:http://api.jquery.com/jQuery.ajax/

的代碼會是這個樣子:

$.ajax({ 
    dataType: 'jsonp', 
    jsonp: 'jsonp_callback', 
    url: 'https://graph.facebook.com/......', 
    success: function() { 
    // do stuff 
    }, 
}); 

希望幫助。

+0

如何獲得'xmlHttp.responseText'? – kaub0st3r 2013-04-01 18:16:10