找不到AMP-Access-Control-Allow-Source-Origin标头

时间:2018-02-21 13:55:19

标签: html xmlhttprequest http-headers amp-html accelerated-mobile-page

我有一个amp-list元素,其src是一个包含产品信息的search.php文件。

验证时,虽然我已经设置了一个,但我没有AMP-Access-Control-Allow-Source-Origin标头。这是php文件:

<?php
header('Content-Type: application/json');
// header('AMP-Access-Control-Allow-Source-Origin: http://localhost');

header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: https://www-xt2-extenders-com.cdn.ampproject.org');
header('Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin');
header('AMP-Access-Control-Allow-Source-Origin: https://www.xt2-extenders.com');


// filter parameters
$filter = isset($_REQUEST["filter"])?$_REQUEST["filter"]:"";


$products = array(
          array(
            "name"=>"HXT²",
            "price"=>2050.00,
            "color"=>"blue",
            "description"=>"Super cool super power t-shirt",
            "image"=>"../img/hxt-set-500-1.png"
            ),
          array(
            "name"=>"DXT²",
            "price"=>1980.00,
            "color"=>"yellow",
            "description"=>"Super cool super power t-shirt",
            "image"=>"../img/dxt²-set-500-2.png"
            ),
          array(
            "name"=>"SXT²",
            "price"=>2050.00,
            "color"=>"green",
            "description"=>"Super cool super power t-shirt",
            "image"=>"../img/sxt²-set-500-2.png"
            ),
          array(
            "name"=>"DPXT²",
            "price"=>3350.00,
            "color"=>"red",
            "description"=>"Super cool super power t-shirt",
            "image"=>"../img/dpxt²-set-500.png"
            )
          )

// Filter by subcategory
if(!empty($filter) && !in_array($filter, array('null', 'none'))) {
$filtered = array();
foreach ($products as $product) {
if($product['color']==$filter) {
  $filtered[] = $product;
}
}
 $products = $filtered;
}

header("HTTP/1.0 200 Ok");

和html:

      <!-- amp-list to fetch and display search results -->
  <!-- with amp-bind, the search keywords, filter, and sort options are all bound to the amp-list src URL -->
  <!-- This means that whenever one of them changes, amp-list will update and fetch the new URL -->
  <amp-list credentials="include"
            width="auto" height="600" layout="fixed-height"
            src="/amp/templates/search.php?filter="
            [src]="'/amp/templates/search.php?filter='+(filter||'')">

      <!-- amp-mustache template to display returned results -->
      <template type="amp-mustache">
        <ul class="related-items">
          {{#results}}
            <li>
              <figure class="related-thumb">
                <amp-img src="{{image}}"
                         width="60" height="60" layout="responsive">
                </amp-img>
                <figcaption>
                  <span class="title">{{name}}</span>
                  <span class="price">€{{price}}</span>
                  <span class="description">{{description}}</span>
                </figcaption>
              </figure>
            </li>
          {{/results}}
        </ul>
        {{#empty}}
          <p>No products matched your search terms</p>
        {{/empty}}
      </template>
  </amp-list>

我做错了什么?我在这里已经阅读了其他问题,并尝试修改我的代码,但它没有用。

Here is the validation error in my browser

1 个答案:

答案 0 :(得分:0)

在我的案例中尝试使用此代码解决了一个问题

$domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
$http_origin = isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN'] ? $_SERVER['HTTP_ORIGIN'] : $domain_url;
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: " .$http_origin);
header("AMP-Access-Control-Allow-Source-Origin: " . $domain_url);
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");

希望它也会帮助你