0

This is an AMP html site. I have forms working find on another domain but this is giving unexpected JSON.parse error:

Failed to parse response JSON: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

What JSON data? I don't get it. Might that be set somewhere outside the code I'm writing, like in php.ini, .htaccess or some server file?

I'm befuddled because I have it working just fine on another domain.

<!DOCTYPE html><html amp>
...

<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
...
<body>
<form class="sample-form"
  method="post"
  action-xhr="https://www.example.com/amp_TEST.php"
  target="_top">
  <input type="text" id="theid" name="name" placeholder="Name..." value="my name here" required>
  <input type="submit" value="Submit">

</form>

And, the receiving file, amp_TEST.php

if(!empty($_POST)){
    header("Access-Control-Allow-Credentials: true");
    header("Access-Control-Allow-Methods: Content-Type");
    header("Access-Control-Allow-Origin: https://www-example-com.cdn.ampproject.org");
    header("Access-Control-Allow-Source-Origin: https://www.example.com");
    header("AMP-Access-Control-Allow-Source-Origin: https://www.example.com");
    header("AMP-Access-Control-Allow-Methods: *");
    header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
    header('Access-Control-Allow-Headers', 'Content-Type');
}

echo '<pre>POST: ' . print_r($_POST, true) . '</pre>';
I am Alta
  • 41
  • 7
  • The fix seems to be for your `amp_TEST.php` code to send back a JSON response instead of `'
    POST: ' . print_r($_POST, true) . '
    '` as it’s sending back now. That’s because when you use `action-xhr`, AMP expects a JSON response. So instead change your code to do something like `echo '{ "POST": "' . print_r($_POST, true) . '"}'` or something. And you should also add `header('Content-Type: application/json')`. See the example code at https://blog.arengu.com/how-to-create-implement-forms-amp-pages/
    – sideshowbarker May 31 '19 at 01:46
  • That was it! I wasn't thinking that far. Thanks for the quick reply! Make it a wonderful day/evening. You deserve it. – I am Alta May 31 '19 at 02:55

0 Answers0