我尝试从www.lightmorelight.com提交表单,但在搜索控制台中出现错误:响应必须包含AMP-Access-Control-Allow-Source-Origin标头。我不明白
这是我的代码:
<?php
if(isset($_POST['submit'])){
header("HTTP/2 200");
header("access-control-allow-headers: Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");
header("access-control-allow-credentials: true");
header("access-control-allow-origin: https://lightmorelight.com");
header("amp-access-control-allow-source-origin: https://lightmorelight.com");
header("access-control-allow-methods: POST, GET, OPTIONS");
header("access-control-expose-headers: AMP-Access-Control-Allow-Source-Origin");
$name = $_POST['nume'];
$subject = $_POST['subiect'];
$message = $_POST['mesaj'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$from = "From:". $name ."<". $email .">";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers = "From: ".$name." <".$email.">\r\n";
$headers .= "Reply-To:".$name." <".$email.">\r\n";
$headers .= "Return-Path: lightmorelight.com<iustinbeceneaga86@gmail.com>\r\n";
$body = "$message\n\n
$name
Tel: $tel
Email:$email_field";
mail('iustinbeceneaga86@gmail.com','www.lightmorelight.com - '.$subject,$body,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
$string = "Thank you ".$name." for getting in touch! \\n\\nI appreciate you contacting me about ".$subject.". I will contact you shortly. \\n\\nHave a great day ahead!";
die("<script>alert(\"$string\");</script><script type=\"text/javascript\">window.location = \"https://www.lightmorelight.com/contact/\";</script>");
}
?>
<form action-xhr="#" method="POST" target="_top">
<input type="text" name="nume" placeholder="Nume" required><br/>
<input type="text" name="tel" placeholder="Tel" required><br/>
<input type="text" name="email" placeholder="Email" required><br/>
<input type="text" name="subiect" placeholder="Subiect" required><br/>
<textarea name="mesaj" placeholder="Mesaj..." rows="6" cols="30"></textarea><br/>
<input type="submit" name="submit" value="Trimite mesaj" class="button">
</form>
任何帮助将不胜感激!
答案 0 :(得分:0)
如果您将action-xhr
与amp-form结合使用,则服务器返回的响应必须为JSON。
由于您的响应是HTML,因此收到的错误来自AMP运行时,试图将HTML解析为JSON。