否';访问控制允许来源';标头存在于请求的资源上


No 'Access-Control-Allow-Origin' header is present on the requested resource

在自定义wordpress页面上,我点击我的链接,就会收到这个错误链接也不会打开。点击不会有任何作用。

XMLHttpRequest无法加载mysite/。请求的资源上不存在"Access Control Allow Origin"标头。因此,不允许访问源"mysite"。

其中mysite是我的网站的url。

有人熟悉这个问题吗?

出现错误的页面为:http://wouterleduc.com/publications/

我在我的页面模板上使用了以下代码:`

<?php if (have_posts()) : the_post(); ?>
    <h3 class="h3_serie_overzicht"><?php the_title(); ?><span id="<?php the_title();?>">&nbsp;&nbsp;&nbsp;</span><a href"#" class="serie_overzicht_info" id="<?php the_title(); ?>">Show text</a></h3>
    <div class="serie_overzicht_info_pane"><?php echo the_content(); ?></div>
<div id="serie-overzicht-thumbs">
    <?php $attachments = new Attachments( 'attachments' ); ?>
    <?php if( $attachments->exist() ) : ?>
      <div class="serie_overzicht_row">
        <?php while( $attachments->get() ) : ?>
          <div class="serie_foto">
           <a target="_blank" href="<?php echo $attachments->field( 'link' ); ?>" title="<?php echo $attachments->field( 'title' ); ?>"><img src="<?php echo $attachments->src( 'medium' ); ?>"" title="<?php echo $attachments->field( 'title' ); ?>" alt="<?php echo $attachments->field( 'title' ); ?>" /><br /><span><?php echo $attachments->field( 'title' ); ?></span></a>
          </div>
        <?php endwhile; ?>
      </div>
    <?php endif; ?>

`

我使用Jonathan Christopher插入的附件来显示图像。我在主题中使用了fancybox,但它应该只影响带有rel标签的链接。

问题是您缺少跨源请求标头。我检查如下

curl -I http://wouterleduc.com/publications/
HTTP/1.1 200 OK
Date: Mon, 29 Sep 2014 19:40:11 GMT
Server: Apache/2
X-Powered-By: PHP/5.4.32
X-Pingback: http://wouterleduc.com/xmlrpc.php
Link: <http://wouterleduc.com/?p=2358>; rel=shortlink
Vary: User-Agent
Content-Type: text/html; charset=UTF-8

将以下行添加到您的php代码中

<?php
 header("Access-Control-Allow-Origin: http://mysite");
?>

您可以使用curl来测试它。如果一切正常,那么您的XmlHttpRequest应该可以工作。存在一些安全隐患。我会看看http://enable-cors.org了解更多信息。