因此,我正在尝试设置一个amp-form,它充当一个更大的normal form的前端,但我需要从amp-form中捕获数据,以及我们的分段和分析数据。我尝试用一个中间页面来实现这一点,amp表单将捕获并转发数据,然后将用户和他们的数据转发到完整表单。
我设置了amp表单,简化的代码如下(简化为一个字段,在这里发布,但测试和相同的问题):
<form class="amp-lead-form" method="post" action-xhr="<?php get_site_url() ?>/submit-amp-form.php" target="_top" custom-validation-reporting="show-first-on-submit">
<fieldset style="border: none;">
<label>
<input class="text_input" type="text"
id="first_name"
name="first_name"
placeholder="First Name"
required
pattern="([a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð,.'-])\w+">
<span visible-when-invalid="valueMissing"
validation-for="first_name"><br>Please enter your first name.</span>
<span visible-when-invalid="patternMismatch"
validation-for="first_name">
<br>Please enter just your first name.</span>
</label>
<div style="clear: both;"></div>
<input class="amp-form-submit" type="submit" name="submit" value="GET STARTED">
</fieldset>
</form>中间页面的代码如下所示(这是测试表单、安全性的基本框架,一旦我弄清楚了数据,就会添加我需要的所有其他东西):
<?php
require_once( dirname( __FILE__ ) . '/wp-load.php' );
$domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://".get_site_url();
header("Content-type: application/json"); //I also tried with multipart/form-data and application/x-www-form-urlencoded
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: ". str_replace('.', '-',get_site_url()) .".cdn.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin: " . get_site_url());
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
header("AMP-Redirect-To: ".get_site_url()."/submit-amp-form.php");
header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin");
//dump data to page so I can view
$data = file_get_contents('php://input');
var_dump($data);
var_dump($_POST);
?>现在中间的页面只显示数据,所以我可以看到它是正确发送的,但显示的是:
DATA:
string(0) ""
array(0) {
}这告诉我数据没有被正确地发送或接收。当我将表单的方法更改为get,并将action-xhr更改为action时,它确实发送和接收get数据。我研究了互联网,发现这通常意味着端点没有实现CORS安全,但我相信我在页面上有所有需要的头文件,所以我真的不知道从哪里开始。有什么帮助吗?
发布于 2019-03-23 03:26:54
如果是CORS问题,您会在浏览器的控制台中看到错误,提示您使用CORS。你检查过了吗?你的控制台应该会给你一个很好的提示,错误在哪里。我猜你没有从你的端点得到一个可接受的响应。这可能是由于你最后的var_dumps造成的。
https://stackoverflow.com/questions/55232117
复制相似问题