0

我正在尝试启用 CORS,但它不适合我。我正在使用 apache 运行 centos 6.5。应用wordpress。我试过以下。

重新启动阿帕奇。我仍然得到错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://gateway.spectrumasa.com/dev/mcmap/test-geosml3.kmz. This can be fixed by moving the resource to the same domain or enabling CORS.

我正在尝试在鼠标悬停而不是单击时创建 geoxml3 kml 多边形工具提示

下面是我目前在我的模板上的代码。

<!-- <script type="text/javascript" src="<?=get_site_url(); ?>/wp-content/themes/twentytwelve-child/js/geoxml3.js.1"></script>
<script type="text/javascript" src="<?=get_site_url(); ?>/wp-content/themes/twentytwelve-child/js/ProjectedOverlay.js"></script>-->
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobubble/src/infobubble.js"></script>
<script type="text/javascript" src="http://geoxml3.googlecode.com/svn/branches/polys/geoxml3.js"></script>
<script type="text/javascript" src="http://geoxml3.googlecode.com/svn/trunk/ProjectedOverlay.js"></script>
<?php header('Access-Control-Allow-Origin: *');?>
<script type="text/javascript">
var geoXmlDoc = null;
var map = null;
jQuery(document).ready(function () {
var myOptions = {
        center: new google.maps.LatLng(-19.5968657,-40.7717683),
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var geoXml = new geoXML3.parser({
        map: map,
        singleInfoWindow: true,
        afterParse: useTheData
    });
    geoXml.parse('https://gateway.spectrumasa.com/dev/mcmap/test-geosml3.kmz');
// alert("done init");
}
);
    function useTheData(doc) {
    // Geodata handling goes here, using JSON properties of the doc object
    geoXmlDoc = doc;
    for (var i = 0; i < doc[0].placemarks.length; i++) {
        var placemark = doc[0].placemarks[i];
        polygonMouseover(placemark.polygon,placemark.name);
        // console.log(doc[0].markers[i].title);
        jQuery('#map_text').append(doc[0].placemarks[i].name + ', ');
    }
    };
var ib = new InfoBubble({
          shadowStyle: 0,
          padding: 0,
          backgroundColor: 'white',
          borderRadius: 4,
          arrowSize: 0,
          borderWidth: 1,
          borderColor: 'black',
          disableAutoPan: true,
          hideCloseButton: true,
          arrowPosition: 50,
          arrowStyle: 0
        });
function polygonMouseover(poly, text) {
   google.maps.event.addListener(poly,'mouseover', function(evt) {
     ib.setContent(text);
     ib.setPosition(evt.latLng);
     ib.setMap(map);
     ib.open()
   });
   google.maps.event.addListener(poly,'mouseout', function(evt) {
     ib.close()
   });
}
</script>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no">



<div id="container" class="three_column_middle">
    <div id="content" role="main">
        <?php  if($post->post_parent){?>
        <div class="section_title"><?php echo get_the_title(); ?></div>     
        <?php }?>
        <div class="second-conent-container">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', 'page' ); ?>
                <?php comments_template( '', true ); ?>
            <?php endwhile; // end of the loop. ?>      

            <div id="map_canvas" style="width:600px;height:500px;"></div>

    <div id="map_text"></div>   
        </div>  
    </div><!-- #content -->

</div><!-- #container -->

<?php get_sidebar('right'); ?>
</div></div><!-- #3 column container -->

<?php get_footer(); ?>

在网关上,它只是公共访问的代理查找

ProxyPassReverse /dev/mcmap http://172.16.0.29/mcmap
ProxyPass /dev/mcmap http://172.16.0.29/mcmap
ProxyPassReverse /dev/knowledgemap http://172.16.0.29/mcmap
ProxyPass /dev/knowledgemap http://172.16.0.29/mcmap

阅读http://g00se.org/2013/07/reverse-proxy-with-cors.html后,我添加了

<LocationMatch "/dev/mcmap">
   Header add "Access-Control-Allow-Origin" "*"
   Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
</LocationMatch>
4

3 回答 3

0

这似乎在进入后wqorked

<LocationMatch "/dev/mcmap">
   Header add "Access-Control-Allow-Origin" "*"
   Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
</LocationMatch>

在代理和清除本地服务器缓存。

于 2014-12-02T16:53:45.053 回答
0

我将以下代码放在我的根目录中.htaccess并运行它,试试这个

<IfModule mod_headers.c>

   ##########################################################################
   # 1.) ENABLE CORS PRE-FLIGHT REQUESTS
   # e.g. PUT, DELETE, OPTIONS, ...
   # we need to set Access-Control-Allow-Headers and
   # Access-Control-Allow-Methods for allowed domain(s)
   ##########################################################################

   # first check for pre-flight headers and set as environment variables
   # e.g. header method-a is set here
   SetEnvIf ^Access-Control-Request-Method$ "method-a" METHOD_A
   SetEnvIf ^Access-Control-Request-Headers$ "^Content-Type$" HEADER_A

   # set corresponding response pre-flight headers for allowed domain(s)
   Header set Access-Control-Request-Methods "method-a" env=METHOD_A
   Header set Access-Control-Request-Headers "content-type" env=HEADER_A

   # TODO: add allowed additional pre-flight requests here...

   #########################################################################
   # 2.) ENABLE CORS *SIMPLE REQUESTS* (vs. Pre-Flight Requests from above)
   # e.g. GET, POST and HEAD requests
   # we need to set Access-Control-Allow-Origin header for allowed domain(s)
   # also note that POST requests need to match one of the following
   # Content-Type:
   # 1) application/x-www-form-urlencoded
   # 2) multipart/form-data
   # 3) text/plain
   #########################################################################


   # e.g. origin = https://host-b.local
   SetEnvIfNoCase Origin "https://host-b.local" AccessControlAllowOrigin=$0
   Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin

   # generic regexp match for more flexibel use cases
   #SetEnvIfNoCase Origin "((http(s?))?://(www\.)?(host\-a|host\-b)\.local)(:\d+)?$" AccessControlAllowOrigin=$0
   #Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin

   # TODO: add additional allowed simple requests here...

</IfModule>
于 2014-12-27T06:41:55.883 回答
0

https://gateway.spectrumasa.com/dev/mcmap/test-geosml3.kmz上的 CORS 策略是您需要更新的内容,而不是在您自己的域中,您在该域中提供需要 Spectrumasa.com 的 javascript。

也许你可以镜像内容。

于 2014-12-02T15:50:14.037 回答