这是AMP html网站。我在另一个域上找到了可工作的表单,但这会产生意外的JSON.parse错误:
未能解析响应JSON:SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符
什么JSON数据?我不明白可以在我编写的代码之外的某个地方进行设置吗,例如在php.ini,.htaccess或某些服务器文件中?
我很困惑,因为我让它在其他域上也能正常工作。
<!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>
然后,接收文件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>';