本文:Ajax.net 1.0正式版發布已久,我在第一次嘗試時卻遇到跨域訪問的問題,表現為在頁面的任意位置單擊滑鼠左鍵都會彈出錯誤提示:"Access is denied",搜尋了一番,找到瞭解決方法.
1.將X:\Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\v1.0.61025\MicrosoftAjaxLibrary中的System.Web.Extensions檔案夾複製到添加到項目中.
2.開啟System.Web.Extensions\MicrosoftAjax.js檔案,在其中找到case Sys.Browser.InternetExplorer節,將其內容替換為下面的代碼:
switch(Sys.Browser.agent){case Sys.Browser.InternetExplorer:Sys.UI.DomElement.getLocation=function(a){if(a.self||a.nodeType===9)return new Sys.UI.Point(0,0);var b=a.getBoundingClientRect();if(!b)return new Sys.UI.Point(0,0);var c=a.document.documentElement,d=b.left-2+c.scrollLeft,e=b.top-2+c.scrollTop;try{var g=a.ownerDocument.parentWindow.frameElement||null;if(g){var f=2-(g.frameBorder||1)*2;d+=f;e+=f}}catch(h){}return new Sys.UI.Point(d,e)};break;
注意以上代碼要寫在一行中.
3.開啟System.Web.Extensions\MicrosoftAjax.debug.js,在其中找到case Sys.Browser.InternetExplorer節,將其內容替換為下面的代碼:
Sys.UI.DomElement.getLocation = function(element) {
if (element.self || element.nodeType === 9) return new Sys.UI.Point(0,0);
var clientRect = element.getBoundingClientRect();
if (!clientRect) {
return new Sys.UI.Point(0,0);
}
var ownerDocument = element.document.documentElement;
var offsetX = clientRect.left - 2 + ownerDocument.scrollLeft,
offsetY = clientRect.top - 2 + ownerDocument.scrollTop;
try {
var f = element.ownerDocument.parentWindow.frameElement || null;
if (f) {
var offset = 2 - (f.frameBorder || 1) * 2;
offsetX += offset;
offsetY += offset;
}
}
catch(ex) {
}
return new Sys.UI.Point(offsetX, offsetY);
}
break;
4.為ScriptManager添加ScriptReference,引用到我們修改過的js檔案上:
<asp:ScriptManager ID="ScriptManager1" runat="server"><Scripts>
<asp:ScriptReference
Name="MicrosoftAjax.js" ScriptMode="Auto"
Path="~/[WebAppPath]/System.Web.Extensions/1.0.61025.0/MicrosoftAjax.js"/>
</Scripts></asp:ScriptManager>
參考文章-1:http://blog.darkthread.net/blogs/darkthreadtw/archive/2007/04/13/kb-cross-domain-access-denied-issue-of-asp-net-ajax-page.aspx
參考文章-2:http://weblogs.asp.net/bleroy/archive/2007/01/31/how-to-work-around-the-quot-access-denied-quot-cross-domain-frame-issue-in-asp-net-ajax-1-0.aspx
[來自Ajax.net 1.0跨域訪問錯誤的解決方案]
類別: JS/HTML/CSS/XML
發布日期: 2007-6-8 16:59