错误响应必须包含AMP-Access-Control-Allow-Source-Origin标头

时间:2018-01-18 12:07:18

标签: php forms http-headers cross-domain amp-html

响应标题

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token
Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Origin:https://www-example-com.cdn.ampproject.org
AMP-Access-Control-Allow-Source-Origin:https://www.example.com
AMP-Control-Expose-Headers:AMP-Access-Control-Allow-Source-Origin
AMP-Same-Origin:true
Cache-Control:no-cache
Content-Length:3
Content-Type:text/html; Charset=utf-8
Date:Thu, 18 Jan 2018 11:47:52 GMT
Server:Microsoft-IIS/8.5
X-Powered-By:ASP.NET
X-Powered-By-Plesk:PleskWin

请求标题

Accept:application/json
Accept-Encoding:gzip, deflate, br
Accept-Language:tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7
Connection:keep-alive
Content-Length:707
Host:www.example.com
Origin:https://www-example-com.cdn.ampproject.org
Referer:https://www-example-com.cdn.ampproject.org/v/s/www.example.com/amp/gc/hbt.html?usqp=adadadas&amp_js_v=0.1
User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1

查询字符串参数

__amp_source_origin:https://www.example.com

无效 https://www.google.com.tr/amp/s/www.example.com/amp

工作 www.example.com/amp

错误代码

  • 响应必须包含AMP-Access-Control-Allow-Source-Origin标头
  • 表单提交失败:错误:响应必须包含AMP-Access-Control-Allow-Source-Origin标头

等待你的帮助

1 个答案:

答案 0 :(得分:0)

在PHP中我们可以这样做:

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("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/替换为您想要的网址