代码之家  ›  专栏  ›  技术社区  ›  Asim Khan

AMP访问控制允许源站标题问题

  •  6
  • Asim Khan  · 技术社区  · 8 年前

    加载资源失败:服务器响应状态为500(内部服务器错误) cdn。AMP项目。组织/v0。js:68响应必须包含AMP Access Control Allow Source Origin标头 Yd@cdn。AMP项目。组织/v0。js:68 cdn。AMP项目。组织/v0。js:68表单提交失败:错误:响应必须包含AMP Access Control Allow Source Origin标头

    AMP GitHub Page on CORS .

    enter image description here

    3 回复  |  直到 6 年前
        1
  •  10
  •   Bachcha Singh    8 年前

    请尝试以下代码

    if(!empty($_POST)){
            $domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
            header("Content-type: application/json");
            header("Access-Control-Allow-Credentials: true");
            header("Access-Control-Allow-Origin: ". str_replace('.', '-','https://example.com') .".cdn.ampproject.org");
            header("AMP-Access-Control-Allow-Source-Origin: " . $domain_url);
            header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
            header("AMP-Redirect-To: https://example.com/thankyou.amp.html");
            header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin"); 
            echo json_encode(array('successmsg'=>'data post'));
            exit;
    }
    

    https

    代替 https://example.com/

        2
  •  1
  •   TrieuNomad    6 年前

    虽然OP使用PHP,但我在这里发布我的javascript页面。

    对于 amp-toolbox-cors

    const express = require('express');
    const ampCors = require('amp-toolbox-cors');
    
    const app = express();
    
    // That's it!
    app.use(ampCors());
    ...
    

    默认情况下,AMP CORS中间件将只允许来自上列出的AMP缓存的请求 https://cdn.ampproject.org/caches.json (添加了bing amp.com)。

    所有其他来源将收到403回复。

    或者开发测试,你们可能还想添加以下内容:

    app.use(ampCors({
      verifyOrigin: false
    }));
    

    AMP CORS .

        3
  •  0
  •   Nitin Zadage frenchomatic    5 年前

    请注意,在上述公认的答案中,如果 https://example.com

    https://www.my-domain.com 需要变成https://www-my--domain-com但事实并非如此。

    header("Access-Control-Allow-Origin: ". str_replace('.', '-','https://www.my-domain.com') .".cdn.ampproject.org");
    

    需要

    header("Access-Control-Allow-Origin: https://www-my--domain.com.cdn.ampproject.org");
    

    所以

    $h = 'https://www.my-domain.com';
    $h = str_replace('-', '--',$h);
    $h = str_replace('.', '-',$h).'.cdn.ampproject.org';
    

    然后

    header("Access-Control-Allow-Origin: " . $h);
    
    推荐文章