编辑:
我更改了表单以提交到名为amp-form-submit.php的新文件
该文件如下:
<?php
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin:".$_SERVER['HTTP_ORIGIN']);
header("AMP-Access-Control-Allow-Source-Origin: https://".$_SERVER['HTTP_HOST']);
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin");
if(!empty($_POST["form_submit"])){
$domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
header("Content-Type: application/json");
$email = isset($_POST['email']) ? $_POST['email'] : '';
$output = ['email' => $email];
header("Content-Type: application/json");
echo json_encode($output);
exit;
}
?>
现在我的错误是Failed to parse response JSON: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
我不明白我做错了什么。下面是我测试放大器形式的代码。我包括了AMP-Access-Control-Allow-Origin标题。
这是失败表单提交的控制台:
POST https://example.com/location/amp-lp/?__amp_source_origin=https%3A%2F%2Fexample.com 404 ()
Response must contain the AMP-Access-Control-Allow-Source-Origin header
Form submission failed: Error: Response must contain the AMP-Access-Control-Allow-Source-Origin header
我已将通用网址替换为隐私权限。此表单处理脚本与doctype html之前的HTML amp-form位于同一页面上。
if ( isset($_POST['form_submit']) ) {
$name = isset($_POST['name']) ? $_POST['name'] : '' ;
$output = [
'name' => $name
];
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: https://www.example.com");
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
echo json_encode($output);
die();
}
我的表格
<form id="lp-form" target="_blank" action-xhr="https://example.com/location/url/" method="post">
<div submit-success>
Thank you! Your message has been sent.
</div>
<div submit-error>
An error occurred. Please try again.
</div>
...
<!-- inputs -->
</form>
答案 0 :(得分:0)
我使用找到的答案here修复了它。 我使用PHPMailer发送电子邮件,而我正在使用
die('MF000');
发送电子邮件后,所有标题都设置为AMP表单,但这会触发以下错误:
无法解析响应JSON:SyntaxError:JSON.parse:JSON数据的第X行X列的数据意外结束
要修复此错误,我只需删除旧模具并使用
die();
如果您仍想解析MF000返回码,请使用:
echo(json_encode(['phpmailer_status'=>'MF000']));