【问题标题】:Response must contain the AMP-Access-Control-Allow-Source-Origin header (I have it)响应必须包含 AMP-Access-Control-Allow-Source-Origin 标头(我有)
【发布时间】: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


【解决方案1】:

我使用找到的答案 here 修复了它。 我正在使用 PHPMailer 发送电子邮件,我正在使用

die('MF000');

在发送电子邮件并为 AMP 表单设置所有标头后,但这会触发以下错误:

解析响应 JSON 失败:SyntaxError: JSON.parse: JSON 数据的 X 行 X 列的数据意外结束

为了修复这个错误,我简单地移除了旧模具并简单地使用

die();

如果你还想解析 MF000 返回码,使用:

echo(json_encode(['phpmailer_status'=>'MF000']));

【讨论】:

    猜你喜欢
    • 2019-05-25
    • 2018-06-27
    • 2018-05-18
    • 1970-01-01
    • 2018-08-01
    • 2015-05-04
    • 1970-01-01
    • 2017-12-28
    • 2019-10-10
    相关资源
    最近更新 更多