【问题标题】:How to fix contact form errors on submit in AMP?如何修复在 AMP 中提交时的联系表单错误?
【发布时间】:2019-01-24 08:13:10
【问题描述】:

我尝试从 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>

任何帮助将不胜感激!

【问题讨论】:

  • 有什么错误?
  • @abielita 无法解析响应 JSON:SyntaxError: Unexpected token

标签: amp-html contact-form amp-form


【解决方案1】:

如果您将 action-xhramp-form 一起使用,则从您的服务器返回的响应必须是 JSON。

由于您的响应是 HTML,因此您收到的错误来自 AMP 运行时试图将此 HTML 解析为 JSON。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2018-12-24
    • 2020-12-25
    • 1970-01-01
    相关资源
    最近更新 更多