响应必须包含AMP-Access-Control-Allow-Source-Origin标头(我有) [英] Response must contain the AMP-Access-Control-Allow-Source-Origin header (I have it)

查看:101
本文介绍了响应必须包含AMP-Access-Control-Allow-Source-Origin标头(我有)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更改了表单以提交到名为amp-form-submit.php的新文件

I changed my form to submit to a new file called 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标头.

I don't understand what I'm doing wrong. Below is my code to test my amp-form. I included the AMP-Access-Control-Allow-Origin header.

这是提交失败表单的控制台:

This is console on failed form submit:

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.此表单处理脚本与我的HTML amp-form在同一页面上,文档类型为html.

I've replaced the URL with generic for privacy. This form processing script is on the same page as my HTML amp-form before doctype html.

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>

推荐答案

我使用找到的答案这里修复了它. 我正在使用PHPMailer发送电子邮件,而我正在使用

I fixed it using an answer found here. I'm using PHPMailer to send emails and I was using

die('MF000');

在发送电子邮件并为AMP Form设置了所有标头之后,但这会引发以下错误:

after the email was sent and all the header were set for AMP Form, but this trigger the following error:

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

Failed to parse response JSON: SyntaxError: JSON.parse: unexpected end of data at line X column X of the JSON data

要修复此错误,我只是移除了旧模具并简单使用了

To fix this bug, I simply removed the old die and simply used

die();

如果您仍然想解析MF000返回代码,请使用:

If you still want to parse the MF000 return code, use:

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

这篇关于响应必须包含AMP-Access-Control-Allow-Source-Origin标头(我有)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆