使用指南https://www.ampproject.org/docs/fundamentals/amp-cors-requests#testing-cors-in-amp测试AMP缓存中的表单我在控制台中收到以下错误但是没有在域中获取它。
阻止表单提交给''因为表格的框架是沙盒的,并且“允许形式”是未设置权限。
表格
<form method="post" class="p2 i-amphtml-form" action-
xhr="https://******.co.uk/form_post.php" target="_top"
novalidate="">
<div class="ampstart-input inline-block relative m0 p0 mb3">
<input type="text" class="block border-none p0 m0" name="name"
placeholder="name ..." required="">
<input type="email" class="block border-none p0 m0" name="email"
placeholder="email ..." required=""></div>
<input type="submit" value="Subscribe" class="ampstart-btn caps">
<div submit-success="">
<template type="amp-mustache">
Success!
</template>
</div>
<div submit-error="">
<template type="amp-mustache">
Error!
</template>
</div>
</form>
form_post.php
<?php
if(!empty($_POST))
{
$name = $_POST['name'];
$email = $_POST['email'];
$to = "*******.co.uk";
$subject = "Website Enquiry";
$txt = $name;
$headers = array(
'From: ' . $email . '' ,
'Reply-To: ' . $email . '' ,
'X-Mailer: PHP/' . phpversion() ,
'MIME-Version: 1.0' ,
'Content-type: text/plain; charset="UTF-8"'
);
$headers = implode( "\r\n" , $headers );
/*/ this is the email we get from visitors*/
$domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") .
"://$_SERVER[HTTP_HOST]";
/*//-->MUST BE 'https://';*/
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin: ".$domain_url);
/*/ For Sending Error Use this code /*/
if(!mail("rob@webifex.co.uk" , "Site Enquiry" , "email: $name <br/>
name: $name" , "From: $name\n ")){
header("HTTP/1.0 412 Precondition Failed", true, 412);
echo json_encode(array('errmsg'=>'There is some error while sending
email!'));
die();
}
else
{
/*/--Assuming all validations are good here--*/
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-
Source-Origin");
mail($to,$subject,$txt,$headers);
echo json_encode(array('successmsg'=>$_POST['name'].'My success
message. [It will be displayed shortly(!) if with redirect]'));
die();
}
}?>
答案 0 :(得分:1)
由于某种原因,错误消息消失但我留下了以下新错误
'Access-Control-Allow-Origin'标头包含无效值'* .ampproject.org'。起源'article-webiflex-co-uk.cdn.ampproject.org';因此不允许访问。
似乎使用wilcard * .ampproject.org'会产生此错误,因此我已更换并正在使用,