【发布时间】:2018-07-15 14:16:03
【问题描述】:
编辑:
我将表单更改为提交到名为 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-form 的代码。我包含了 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
出于隐私考虑,我已将 URL 替换为通用 URL。此表单处理脚本与我在 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>
【问题讨论】:
-
它没有。同样的错误。
标签: php http-headers amp-html