代码改变世界

Ajax.net 1.0跨域访问错误的解决方法

2007-06-08 16:27  Windie Chai  阅读(1865)  评论(0编辑  收藏  举报

 

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

977858d0