我使用Amp发布表单并使用PHP进行处理。我收到了以下错误:
表单提交失败::响应必须包含AMP-Access-Control-Allow-Source-Origin标头
代码:
<?php
if(isset($_POST\['submitlogin'\]))
{
$name = isset($_POST\['name'\]) ? $_POST\['name'\] : '' ;
$output = \[
'name' => $name
\];
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("AMP-Same-Origin: true");
header("Access-Control-Allow-Origin: *.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin: *");
#header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
echo json_encode($output);
die();
}
?>
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js">
</script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js">
</script>
<link rel="canonical" href="localhost">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<title>AMP form</title>
</head>
<body>
<form method="post" action-xhr="#" target="_top">
Name:<input type="text" name="name" />
<input type="submit" name="submitlogin" value="Submit" />
</form>
<div submit-success>
<template type="amp-mustache">
Success! Thanks for trying the
<code>amp-form</code> demo! The name submited was {{name}}
</template>
</div>
</body>enter code here
</html>
我已经包含了所有标头,我面临的唯一问题是该网站是使用Apache托管在localhost上的。请帮助我做我需要做的事情,在localhost上允许放大器标题。
答案 0 :(得分:2)
尝试将以下行添加到.htaccess:
Header set Access-Control-Allow-Origin "*"
Header set AMP-Access-Control-Allow-Source-Origin "https://yourdomain.com"
答案 1 :(得分:1)
我在寻找相同的答案时找到了你的问题。在调试请求时,我注意到以下查询字符串已添加到请求
?__amp_source_origin=http%3A%2F%2Flocalhost%3A8080
并添加
header('AMP-Access-Control-Allow-Source-Origin: '.'http://'. $_SERVER['HTTP_HOST']);
对于回应,AMP停止抱怨。