简体   繁体   English

使用XDomainRequest调用跨域AJAX时权限被拒绝错误

[英]Permission denied error when invoking cross domain AJAX using XDomainRequest

I have created a php page that makes use of Google Javascript APIs. 我创建了一个使用Google Javascript API的php页面。 I am trying to use Ajax to populate the info windows of the markers dynamically, but get a permission denied error on the xdr.open line. 我试图使用Ajax动态填充标记的信息窗口,但在xdr.open行上获得权限被拒绝错误。 I have checked the other posts relating to similar errors, and tried different changes, but still the same error. 我检查了与类似错误有关的其他帖子,并尝试了不同的更改,但仍然是同样的错误。

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>

<script type=text/javascript>
var marker;
var map;
var xdr;

function loader()
{
        alert("XDR onload");
        alert("Got: " + xdr.responseText);
}

function Info(pmarker)
{
    google.maps.event.addListener(pmarker, 'mouseover', function()
    {
     if (window.XDomainRequest)
     {
        xdr = new XDomainRequest();
        if(xdr)
        {
            document.write('1');

            xdr.open("GET", "http://localhost/GMap/GroovyCaller.php?Node=Host1");
            xdr.send();
            document.write('2');
            xdr.onload = loader;
        }

        else
        {
            document.write('3');
            alert('Failed to create');
        }
     }
     else
     {
          document.write('4');
          alert('XDR does not exist');
     }
});
}
function createMarker() {

    var myLatlng = new google.maps.LatLng(-34.397, 150.644);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title:"Hello World!"
        });
        Info(marker);
}
function initialize()
{
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions =
    {
        zoom: 8,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);


    createMarker();

}


</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>

I had a similar issue to this where I had client-side code running on a local domain (eg www.domain.local ) that was trying to contact a server at localhost . 我有一个类似的问题,我在本地域(例如www.domain.local )上运行客户端代码,试图联系localhost的服务器。 Even when I added Access-Control-Allow-Origin: * to the servers response headers I was still getting Access Denied. 即使我在服务器响应头中添加了Access-Control-Allow-Origin: * ,我仍然得到Access Denied。

Turns out XDomainRequest does not like trying to access localhost from another domain. 事实证明XDomainRequest不喜欢尝试从另一个域访问localhost。 See point 6 of http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx . 请参阅http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx的第6点。

My solution was create another fake domain pointing to localhost using my hosts file and then pointing XDR at that new domain instead. 我的解决方案是创建另一个伪域,使用我的hosts文件指向localhost,然后将XDR指向该新域。

Is GroovyCaller.php returning the http header Access-Control-Allow-Origin ? GroovyCaller.php是否返回http标头Access-Control-Allow-Origin From http://msdn.microsoft.com/en-us/library/cc288060(v=vs.85).aspx : http://msdn.microsoft.com/zh-cn/library/cc288060(v=vs.85).aspx

Cross-domain requests require mutual consent between the document and the server. 跨域请求需要文档和服务器之间的相互同意。 [...] It will only complete the connection if the server responds with an Access-Control-Allow-Origin header of either * or the exact URL of the requesting document. [...]仅当服务器使用*或请求文档的确切URL的Access-Control-Allow-Origin标头响应时,它才会完成连接。

Your server must know about your client to accept a cross domain connection. 您的服务器必须知道您的客户端才能接受跨域连接。 You can start with Access-Control-Allow-Origin: * to get it working, and then narrow it down. 您可以从Access-Control-Allow-Origin: *开始使其工作,然后缩小范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM