代码之家  ›  专栏  ›  技术社区  ›  rjovic

xmlhttpRequest open()返回拒绝访问

  •  4
  • rjovic  · 技术社区  · 14 年前

    我对xhr open()方法有问题。我的代码如下:

    var xmlhttp=false;
    
    if(!xmlhttp)
        try
        { 
            xmlhttp=new XMLHttpRequest(); 
        }
        catch(e)
        {
            xmlhttp=false;
        }
    
    function returnPage(url)
    {
        if(!xmlhttp)
            return alert("Your browser doesn't seem to support XMLHttpRequests.");
    
        xmlhttp.open("GET",url,true);
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState!=4) return;
            if(!xmlhttp.status||xmlhttp.status==200)
                alert(xmlhttp.responseText);
            else
                alert("Request failed!");
        }; //onreadystatechange
    
        xmlhttp.send(null);
    }
    

    呼叫:

    <a href='#' onclick="returnPage('http://www.something.com'); return false;">Link 1</a></p>
    

    我正在使用IE8(因为我正在构建网页快讯),收到错误“拒绝访问”。我在互联网上发现问题是XHR不能跨域工作,但我使用了来自Firefox插件的代码,它工作正常。这个附加组件和“我的”代码(相同)正在调用同一个页面。那个附加组件是如何访问的,而我的代码不是?

    1 回复  |  直到 14 年前
        1
  •  6
  •   Matt    14 年前

    您对Ajax调用的域是否与您的站点所在的域相同?不能向其他域发出请求。

    编辑 :

    火狐附加组件具有更高的权限(因为用户必须安装它们)。这就是为什么外接程序可以发出跨域请求。