0

我有 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>

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

这是我浏览器中的验证错误

4

1 回答 1

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");

希望它也能帮助你

于 2018-02-22T15:23:40.613 回答