Response must contain the AMP-Access-Control-Allow-Source-Origin header (I have it)

南笙酒味 提交于 2019-12-13 03:18:45

问题


EDIT:

I changed my form to submit to a new file called amp-form-submit.php

That file looks like:

<?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;
}
?>

Now my error is Failed to parse response JSON: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data


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​​​

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();

}

my form

       <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>

回答1:


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

die('MF000');

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

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();

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

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


来源:https://stackoverflow.com/questions/48627403/response-must-contain-the-amp-access-control-allow-source-origin-header-i-have

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!